Help creating a non-intrinsic smoothing function.
Ältere Kommentare anzeigen
So I have to create a smoothing function of some random data that roughly plots a cosine wave. I have been given the following pseudocode to work with but am having trouble interpreting the second and third, for's:
- If width=5 and length(x)=100, then for n=3:98, smoothed(n)=mean(x(n-2:n+2)); for n<3, smoothed(1)=x(1); smoothed(2)=mean(x(1),x(2),x(3)); for n>98, smoothed(99)=mean(x(98),x(99),x(100)); smoothed(100)=x(100).
This works for the first for but when it generates a vector, its first two values are 0 and the last 2 arent even included.
Here is the current code I have:
t = linspace(0,1,100); noise = rand(1,length(t)); x = cos(2*pi*t) + 0.5*(rand(size(noise))-0.5); width=5; smoothed=CTask2p1_f(x, width) figure hold on plot(t,x,'bo') plot(t,smoothed,'r-')
and my function
function smoothed = CTask2p1_f(x,width)
if width == 5 && length(x) == 100
for n=3:98
smoothed(n)=mean(x(n-2:n+2));
end
while n < 3
smoothed(1)=x(1);
smoothed(2)=mean(x(1),x(2),x(3));
end
while n > 98
smoothed(99)=mean(x(98),x(99),x(100));
smoothed(100)=x(100);
end
endI am not sure what is causing the issue here. I have two while loops as an error occurs if I use for instead of while on the greater than less than parts. Help will be greatly appreciated.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Kernel Distribution finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!