Smooth vector data with a specific step

5 views (last 30 days)
Lev Mihailov
Lev Mihailov on 4 Apr 2022
Commented: Mathieu NOE on 6 Apr 2022
I need to smooth the data, but the smooth function flips the response of the data, which makes things a little more difficult
x = rand(1,4097);
y = rand(1,4097);
z1=smooth(x(1:10),x(1:10),0.8,'rloess')'; % len(1,10)
z2=smooth(x(11:21),x(11:21),0.8,'rloess')'; % len(,10)
z=[z1,z2] % for each part z=[z1,z2... z(end)]
% the code that I am currently using, but I do not understand why such an answer
step = 10;
for j = 1:step:size(x,2)-step
a = y(j:j+1);
b = x(j:j+1);
z(1,j+1)= smooth(a,b,0.8,'rloess')';
end
The answer "z" is not correct (it's a 2,4097 matrix) and there are a lot of zeros in the data.
Thank you in advance

Answers (1)

Mathieu NOE
Mathieu NOE on 4 Apr 2022
hello
not sure what you are trying to do , but if it's about splitting data in buffers and doing some math on the buffers (mean , rms, smooth,..), here maybe some code to help you :
clearvars
% dummy data
n=4097;
x= 1:n;
y = sin(2*pi*x./max(x))+rand(1,n);
buffer = 10; % nb of samples in one buffer (buffer size)
overlap = 5; % overlap expressed in samples
%%%% main loop %%%%
m = length(y);
shift = buffer-overlap; % nb of samples between 2 contiguous buffers
for ci=1:fix((m-buffer)/shift +1)
start_index = 1+(ci-1)*shift;
stop_index = min(start_index+ buffer-1,m);
time_index(ci) = round((start_index+stop_index)/2); % time index expressed as sample unit (dt = 1 in this simulation)
mean_data(ci) = mean(y(start_index:stop_index)); %
end
x_mean = x(time_index);
figure(1),
plot(x,y,x_mean,mean_data,'r');
  3 Comments
Mathieu NOE
Mathieu NOE on 6 Apr 2022
hello Lev
funny, I have no trouble on my release (R2020b)
I just changed now the variable name buffer to mybuffer to avoid potential conflict with matlab's buffer function
clearvars
% dummy data
n=4097;
x= 1:n;
y = sin(2*pi*x./max(x))+rand(1,n);
mybuffer = 10; % nb of samples in one buffer (buffer size)
overlap = 5; % overlap expressed in samples
%%%% main loop %%%%
m = length(y);
shift = mybuffer-overlap; % nb of samples between 2 contiguous buffers
for ci=1:fix((m-mybuffer)/shift +1)
start_index = 1+(ci-1)*shift;
stop_index = min(start_index+ mybuffer-1,m);
time_index(ci) = round((start_index+stop_index)/2); % time index expressed as sample unit (dt = 1 in this simulation)
mean_data(ci) = mean(y(start_index:stop_index)); %
end
x_mean = x(time_index);
figure(1),
plot(x,y,x_mean,mean_data,'r');
legend('signal','buffered mean');
Mathieu NOE
Mathieu NOE on 6 Apr 2022
maybe check that you have not used or initialized some of my code's variables from another portion of code that would change arrays dimensions (from my code version)

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!