2 views (last 30 days)

Commented: 洪宇 on 20 Jun 2024
You can see that my original signal is the yellow line in the picture, similar to a series of pulses. I want to process the original signal to look like the red in the picture, a ramp signal with a class period. After a peak, it immediately decreases to 0 and then linearly increases from 0 to a new peak
i use matlab function in simulink ，but can‘t work like i expect
my english is not that good ，if you have any question,please let me know

function processed_signal = process_signal(peak_signal)
num_samples = length(peak_signal); % 获取输入信号的长度
processed_signal = zeros(num_samples, 1); % 初始化输出信号为零
% 找到峰值点
peak_times = find(peak_signal); % 找到所有峰值点的时间索引
if isempty(peak_times)
return; % 如果没有峰值点，直接返回
end
% 线性上升到达每个峰值点
for i = 1:length(peak_times)
peak_index = peak_times(i);
peak_height = peak_signal(peak_index);
% 计算线性上升的斜率
slope = peak_height / peak_index; % 斜率 = 峰值高度 / 达到峰值的点数
% 应用线性上升
for j = 1:peak_index
processed_signal(j) = slope * j;
end
end
% 由于在每个峰值点后信号立即下降到0，我们不需要额外的代码来设置为0
end

this is my code, it can run ,but the output in scope block is the same as my original signal

Fangjun Jiang on 19 Jun 2024
Edited: Fangjun Jiang on 19 Jun 2024
It does not work like that. A MATLAB Function block is like any other Simulink block. It is executed at every time step. At t=5, it is not supposed to know the peak value of the input sigal at t=6. You could provie all the peak values of the input signal for the entire 30 seconds at once, but again, do you want the MATLAB Function block to do the same processing again and again at every simulation step?
I suggest using the Signal Builder block. Add a sawtooth signal, then you can adjust those data points (either x or y value) to match your data.
If there are too many data points, then you can use a Lookup Table block to specify the data. Then providing the Clock signal as the input and the output of the Lookup Table block will be the signal you need. The only issue is that you need to specify the time for the peak and then the time for the "zero" be really close.

thanks a lot

### Categories

Find more on Signal Attributes and Indexing in Help Center and File Exchange

R2023a

### Community Treasure Hunt

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

Start Hunting!