MATLAB Answers

How to replace (in a vector ) every value by the average of the previous n samples? (To smooth a signal)

1 view (last 30 days)
Victoria Miller
Victoria Miller on 27 May 2020
Answered: Ameer Hamza on 27 May 2020
Hi,
I need help with a code that replaces every value by the average of the previous 100 samples.
And still remains in the same length that it was before..
I tried this:
% resampling - smoothing, replacing each value in the mean of previous last
n = 100; % number of elements to create the mean over
num1 = length(SE_rest);
num2 = mod(num1,n);
SE_rest_resampled = SE_rest(1:num1-num2,1);
samples = reshape(SE_rest_resampled,n,length(SE_rest_resampled)/n); % reshape SE_rest_resampled to [n, (num1-num2)/n)
energies = ((sum(samples.*samples)));
but it changes the length of the vector, and I need it to stay in the same length - and just to be smoother.
Thanks !!!

  0 Comments

Sign in to comment.