help-octave
[Top][All Lists]

## RE: Smoothing a roughly sinusoidal signal

 From: Windhorn, Allen E [ACIM/LSA/MKT] Subject: RE: Smoothing a roughly sinusoidal signal Date: Thu, 7 Nov 2019 19:20:02 +0000

```> -----Original Message-----
> From: Help-octave <help-octave-
>
> <https://octave.1599824.n4.nabble.com/file/t373550/data.csv>  . The data
> format is as follows (only the first 6 values are relevant):
>
> time, pos1, posErr1, pos2, posErr2, (posErr2 - posErr1), ignore, ignore

Moving average seems to work well.  There is one in the financial package, but I
found it did not suit my needs, so wrote my own:
---------------------------------------------------------------------------------------
function ym = mavg(y,m)
%MAVG Moving average of m points of y
% mavg(y,m) -- Compute moving average of y over m points
# (works better if m is odd).  For first and last m/2 points
# uses linear fit.
% At the moment, y must be a column vector of one dimension.
%
% Allen Windhorn, ECE504, Summer 1999
%
n = length(y);
if m <= 1 | n < 3                   %Pathological cases
ym = y;                           %Return same array
return;
end
%
if m > n,m = n;end                   %Array too short for m
mh = fix(m/2+0.01);                  %Half length of array, rounded down
mh1 = m-mh;
%Compute the end regions
ym(1) = (2*y(1)+y(2))/3;
ym(n) = (2*y(n)+y(n-1))/3;
for k = 2:mh
ym(k) = sum(y(1:(2*k-1)))/(2*k-1);
ym(n-k+1) = sum(y((n-2*k+2):n))/(2*k-1);
end
%
%Now compute the points in between using moving average
for k = mh+1:n-mh1+1
ym(k) = sum(y(k-mh:k+mh1-1))/m;
end
-------------------------------------------------------------------------------------------
With the above on your path, you can do:

t = data(:,1);
u = data(:,3);
plot(t, u, t, mavg(u, 2400));  %Average over 24 seconds

Regards,
Allen

```