Matlab to Simulink Conversion

1 次查看(过去 30 天)
kat001
kat001 2018-6-15
Hi, I have this following code:
x = -5:0.0001:5;
numPoints = length(x);
norm = normpdf(x,0,0.3925);
%%Assigning Vectors
t = 0:1:100; %time
xi = 1; % initial position of x
xPos = zeros(1, length(t)); % creates 100 columns containing zeros
normMax = norm(1) * ones(1, length(t)); % creates 100 columns containing norm(1)
xPosMax = zeros(1, length(t));
normT = zeros(1, length(t));
%initial movement
xPos(1)=xi;
normT(1) = norm(find(abs(x-xi)<min(diff(x))/2)); % provides values one step before and after the max. of norm
xi=xi+0.1;
xPos(2)=xi;
normT(2) = norm(find(abs(x-xi)<min(diff(x))/2));
%%Power Comparison & Tracking Algorithm
for k = 3:length(t)
if(normT(k-1)>normT(k-2))
xi = xi + (xPos(k-1) - xPos(k-2)); % position increment x axis
else
xi =xi - (xPos(k-1) - xPos(k-2)); % position decrement x axis
end
xPos(k) = xi; % saving increment/decrement of position in xPos for each step
normT(k) = norm(find(abs(x-xi)<min(diff(x))/2));
end
%%Plot
plot(t, xPos, 'r-', 'LineWidth', 2);
ylabel('X Position (Tracking Mode)', 'FontSize', fontSize);
hold on;
plot(t, normT, 'b-', 'LineWidth', 2);
grid on
hold off
ylabel('Counter Terminal Received Power', 'FontSize', fontSize);
title('Peak Power Detection', 'FontSize', fontSize);
xlabel('t', 'FontSize', fontSize);
legend('xPos', 'norm', 'Location', 'east');
And this needs to be programmed in Simulink. The first three line of codes can be neglected since that would be my input anyways as a continuous signal. I have just started like this:
How to proceed from here? Anybody who has good Simulink skills to help me out here?

回答(1 个)

Ameer Hamza
Ameer Hamza 2018-6-16
You can use MATLAB function block to use your MATLAB code in Simulink. Note that this block will process one data point at a time so that it can process the real-time data stream.
  4 个评论
kat001
kat001 2018-6-16
编辑:kat001 2018-6-16
hmm.. I am not sure whether I followed your example. What I was thinking of is that the input signal, which in this case is the power, need to be checked every time step and compared with the previous step. Because, when moving the x-axis, the input power is being affected.
I simply thought by discretizing the input signal, I can compare the current power with the previous power as x axis is moving.
The steps I thought of are following: If the current power is higher than previous power, I apply a memory and hold that current power value and move my x axis until I find next higher power. Eventually, I will get less power and my x axis needs to go back to previous step where it found the maximum power.
I am not sure whether I could explain it very well. But if you run my code, you will see the plot and then it might make sense to you.
The image below is to give you an idea of what I am talking about (the sine wave (x-axis) is just an example to show how the 'pwr' is being affected. The plot shows the pwr:
Ameer Hamza
Ameer Hamza 2018-6-16
From your description, it appears that you need a complete signal for processing since you are moving forward and backward on the signal. You might want to use To Workspace block to export your simulation result to MATLAB workspace and apply the algorithm. But if you are just trying to find the maximum value of the signal and its location at which it occurred then you can try the following function.
function [value, location] = maxSignal(x)
value = 0; % to tell simulink the size of 'y'
location = 0;
persistent maxValue;
persistent count;
persistent maxLocationTemp;
if isempty(maxValue)
maxValue = 0;
end
if isempty(count)
count = 1;
end
if isempty(maxLocationTemp)
maxLocationTemp = 1;
end
if(x > maxValue)
maxValue = x;
maxLocationTemp = count;
end
value = maxValue;
location = maxLocationTemp;
count = count+1;
end
place this code in a MATLAB Function block in Simulink. It will output the maximum value of the signal received thus far and also output the sample number at which it was received.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Event Functions 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by