how to get the envelope of oscillating signal
조회 수: 10 (최근 30일)
이전 댓글 표시
I have a decaying oscllatory signal for which I want to get the envelope to estimate the time constant of the system.
load data.mat
plot(t,il5)
[pks,locs]=findpeaks(il5);
hold
plot(t(locs),pks,'or')
I tried using the 'Minpeakdistance' constraint, set to 40e-6,but that still did not pick the right peaks ---in fact, I didn't see much difference.
[pks2,locs2]=findpeaks(il5,'MinPeakDistance',40e-6);
figure;
plot(t,il5)
hold
plot(t(locs2),pks2,'oc')
Anyway, how do I get the [positive] "envelope" of this signal -- the black line below?
Thanks!
Jorge
댓글 수: 0
채택된 답변
Star Strider
2024년 10월 8일
Perhaps this —
LD = load('data.mat')
t = LD.t;
il5 = LD.il5;
Data = rmmissing([t il5]);
t = Data(:,1);
il5 = Data(:,2);
nc = find(il5 > 0, 1, 'first')-1
figure
plot(t, il5)
hold on
plot(t(nc), il5(nc), '+r')
hold off
grid
tv = t(nc:end);
il5v = il5(nc:end);
[pks,locs] = findpeaks(il5v, MinPeakDistance=150);
figure
plot(tv, il5v)
hold on
plot(tv(locs), il5v(locs), '+r')
hold off
grid
% xlim([0.0049 0.006])
objfcn = @(b,x) b(1).*exp(b(2).*(x + b(3))) + b(4);
[B,rn] = fminsearch(@(b) norm(il5v(locs) - objfcn(b,tv(locs))), [2; -1E3; tv(1); 0])
figure
plot(tv, il5v)
hold on
plot(tv, objfcn(B,tv), '-r', 'LineWidth',1.5)
hold off
xlabel('Time')
ylabel('Amplitude')
grid
text(0.0075, 1.5, sprintf('$il5(t) = %.2f \\cdot e^{%.2f \\cdot (t %+.4f)} %+.4f$', B), 'Interpreter','LaTeX', 'FontSize',14)
.
댓글 수: 3
Star Strider
2024년 10월 9일
As always, my pleasure!
‘What are the units of the 150 in 'MinPeakDistance=150'?’
Those are the index values. I prefer to use index values with findpeaks (and similar functions) because I can then apply them to all necessary variables. (I left out some intermediate calculations and plots where I plotted ‘il5’ against its indices rather than against the time values to understand how the index values worked with it.)
The objective function:
objfcn = @(b,x) b(1).*exp(b(2).*(x + b(3))) + b(4);
is a generic one that generally fits exponential data. This one includes an offset (‘b(3)’) for the inidependent variable because the data do not begin at t=0. I coded it as an anonymous function. See the documentatiion on Anoymous Functions for details.
The fminsearch function is a derivative-free (that is, it does not use gradient-decent) optimisation algorithm that finds the minimum of its function argument. Here, that means subtracting the function value at every value of the independent variable from the dependent variable at every corresponding value of them. When it finds that minimum, then it reports back the parameter vector (‘B’) that corresponds to the minimum. I chose fminsearch because it is part of core MATLAB, so everyone has it.
[B,rn] = fminsearch(@(b) norm(il5v(locs) - objfcn(b,tv(locs))), [2; -1E3; tv(1); 0])
The ‘rn’ (residual norm) is the norm of the reesiduals at convergence (when the function argument value is at its minimum). The function I use calculates the norm of the residuals (essentially the square root of the sum of the squares of its argument, that being the difference between the dependent variable and the function value for both vectors) at each step.
I will gladly enlarge on this discussion of you would like more detail.
.
추가 답변 (1개)
Image Analyst
2024년 10월 9일
help envelope
댓글 수: 3
Image Analyst
2024년 10월 9일
편집: Image Analyst
2024년 10월 10일
Did you look at your data to see if all the values make sense? You have nan's in there. Remove them.
load('jorge data.mat')
plot(t, il5, 'b.');
grid on;
nanIndexes = isnan(il5);
t(nanIndexes) = [];
il5(nanIndexes) = [];
[yupper,ylower] = envelope(il5);
hold on;
plot(t, ylower, 'r-');
plot(t, yupper, 'r-');
Zooming way in you see:
Looks like an envelope to me.
참고 항목
카테고리
Help Center 및 File Exchange에서 Descriptive Statistics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!