Matlab to Simulink Conversion
1 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Event Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!