smoothing with Gaussian Kernel for loop problem

im having a probelm with smoothing the data "ys" using gaussian kernel function everytime i run the for loop i recieve uhat = ys and kerf = 0 0 0 0 0 ... anyone can help me?
ns =length(ys);
nv =length(tv);
lambda = 0.05;
tv = (1:1:1000)';
for i = 1 : ns
k=(tv-tv(i));
kerf=exp(-k.*k/2*(lambda^2))/(sqrt(2*pi)*lambda);
uhat(i)=sum(kerf.*ys(i))/sum(kerf);
end

 採用された回答

Mathieu NOE
Mathieu NOE 2022 年 11 月 18 日

0 投票

hello
try this
lambda = 0.05;
tv = (1:1:1000)';
nv =length(tv);
ys = sin(4*pi*tv./max(tv))+0.25*rand(nv,1);% dummy data
for i = 1 : nv
k=(tv-tv(i));
kerf=exp(-k.*k/2*(lambda^2))/(sqrt(2*pi)*lambda);
uhat(i)=sum(kerf(:).*ys(:))/sum(kerf);
end
plot(tv,ys,tv,uhat);

8 件のコメント

KHOULOUD DABOUSSI
KHOULOUD DABOUSSI 2022 年 11 月 18 日
編集済み: KHOULOUD DABOUSSI 2022 年 11 月 18 日
hello,i receive this error message "Arrays have incompatible sizes for this operation.",if you want this is the data im working on it and tv = (1:1:ts(end))';
Mathieu NOE
Mathieu NOE 2022 年 11 月 21 日
hello again
try this code :
load('test_b.mat')
lambda = cv;
for i = 1 : numel(ts)
k=(ts-ts(i));
kerf=exp(-k.*k/2*(lambda^2))/(sqrt(2*pi)*lambda);
uhat(i)=sum(kerf(:).*ys(:))/sum(kerf);
end
plot(ts,ys,ts,uhat);
Mathieu NOE
Mathieu NOE 2022 年 11 月 21 日
FYI, I made a comparison with matlab smoothdata function
with a gaussian window (length = 10 samples) you get pretty much the same result (the two traces are quite perfectly overlaid)
load('test_b.mat')
lambda = cv;
for i = 1 : numel(ts)
k=(ts-ts(i));
kerf=exp(-k.*k/2*(lambda^2))/(sqrt(2*pi)*lambda);
uhat(i)=sum(kerf(:).*ys(:))/sum(kerf);
end
% comparison with smoothdata function
ysm = smoothdata(ys,'gaussian',10);
plot(ts,ys,ts,uhat,ts,ysm);
legend('raw','exp smoothing','gaussian smoothing');
Mathieu NOE
Mathieu NOE 2022 年 11 月 21 日
well, this is no surprise as both codes implement a gausian window !! :)
KHOULOUD DABOUSSI
KHOULOUD DABOUSSI 2022 年 11 月 21 日
hi,thank you for your help!
Mathieu NOE
Mathieu NOE 2022 年 11 月 21 日
as always, my pleasure !
KHOULOUD DABOUSSI
KHOULOUD DABOUSSI 2022 年 11 月 21 日
by chance,do you have any idea how to select the optimal bandwidth (lambda)? not using ksdensity?
Mathieu NOE
Mathieu NOE 2022 年 11 月 22 日
hello again
well, statistics are not my field of expertise
there are some publications that describe some methods for optimal tuning of lambda
but then i's up to you to code that as a matlab function (would basically be your own version of ksdensity)

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

その他の回答 (0 件)

製品

リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by