Smooth vector data with a specific step

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

回答 (1 件)

Mathieu NOE
Mathieu NOE 2022 年 4 月 4 日

0 投票

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 件のコメント

Lev Mihailov
Lev Mihailov 2022 年 4 月 5 日
The code throws this error
Unable to perform assignment because the left and right sides have a different number of elements.
Error in untitled2 (line 12)
stop_index(ci) = min(start_index+ buffer-1,m);
Mathieu NOE
Mathieu NOE 2022 年 4 月 6 日
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 2022 年 4 月 6 日
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)

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

質問済み:

2022 年 4 月 4 日

コメント済み:

2022 年 4 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by