Omit detrending in certain portion of signal
5 次查看(过去 30 天)
显示 更早的评论
Hello,
I have a signal with a certain portion that is much higher than the rest of the signal.
How can I avoid applying detrending to this portion but apply detrending to everything to the left and right of this signal? Blue portion should be detrended. Red portion should not be detrended and stay at this or a similar level.
What if I have two of these red portions within my signal? For example the lower peak portion in the middle and the higher portion to the right. Do not detrend both portions?
Thanks.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/937569/image.png)
2 个评论
采纳的回答
Mathieu NOE
2022-3-23
hello
may I suggest a small improvement here - this avoid finding manually the samples that shoudn't be detrended
so whatever the amount and x position of the peaks, this code should work :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/938374/image.png)
load('signal.mat');
n = 1:length(signal);
n = n';
signalS = smoothdata(signal,'movmedian',200);
I = abs(signal-signalS) < 5e-3;
signal_detrended = detrend(signal(I), 'SamplePoints', n(I));
signal_combined = zeros(length(signal), 1);
signal_combined(I) = signal_detrended;
signal_combined(~I) = signal(~I);
figure(1), plot(n, signal,n, signalS,n, signal_combined)
legend('raw','smoothed','partial detrend');
更多回答(1 个)
Benjamin Thompson
2022-3-22
You can use an index vector and the 'SamplePoints' option for the detrend function:
>> n = 1:length(signal);
>>n = n';
>> I = (n < 4000) | ((n > 4500)) & (n < 8500);
>> signal_detrended = detrend(signal(I), 'SamplePoints', n(I));
>> hold off;
>> figure, plot(n, signal)
>> grid on, zoom on
>> hold on
>> plot(n(I), signal_detrended, 'r')
2 个评论
Benjamin Thompson
2022-3-22
This is just some basic MATLAB commands, but here you are. The index vector defines the set of what was detrended and what was not.
>> signal_combined = zeros(length(signal), 1);
>> signal_combined(I) = signal_detrended;
>> signal_combined(~I) = signal(~I);
>> figure, plot(n, signal_combined)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!