hello
- basically you want to plot the envelope of your data , there is a function for that :
envelope Envelope detector.
[YUPPER,YLOWER] = envelope(X) returns the upper and lower envelopes of
the input sequence, X, using the magnitude of its analytic signal.
it requires the Signal Processing Toolbox
- you may want to remove first the spikes , so a bit of low pass filtering / smoothing is needed
you can do that with smoothdata
If you don't have the Signal Processing Toolbox, see the demo below :
x = exp(-(t-5).^2/2).*sin(2*pi*5*t);
[up,down] = envelope2(t,x,'linear');
plot(t,x,t,y,'k',t,up,'m',t,down,'c')
legend('signal','hilbert','envelope2 / upper env','envelope2 / lower env');
function [up,down] = envelope2(x,y,interpMethod)
if length(x) ~= length(y)
error('Two input data should have the same length.');
if (nargin < 2)|(nargin > 3),
error('Please see help for INPUT DATA.');
extrMaxIndex = find(diff(sign(diff(y)))==-2)+1;
extrMaxValue = y(extrMaxIndex);
extrMinIndex = find(diff(sign(diff(y)))==+2)+1;
extrMinValue = y(extrMinIndex);
down_x = x(extrMinIndex);
up = interp1(up_x,up,x,interpMethod);
down = interp1(down_x,down,x,interpMethod);