Smoothing data without using inbuilt functions
Show older comments
I have a random set of noisy data and need to smooth it without using inbuilt functions. I believe my function and script are close to achieving this. However, they only work for a width of 5, I am unsure how to change the function in order to work for any width size. Function:
function smoothed = CTask2p1_f (x, width)
% Left
for n = 1:2
smoothed(1) = x(1);
smoothed(2) = mean(x(1):x(3));
end
% Middle
for n = 3:98
W = (width - 1) / 2; % Width should be odd
smoothed(n) = mean (x(n-W:n+W));
end
% Right
for n = 99:100
smoothed(99) = mean(x(98):x(100));
smoothed(100) = x(100);
end
end
Script:
t = linspace(0,1,100);
noise = rand(1,length(t));
x = cos(2*pi*t)+0.5*(rand(size(noise))-0.5);
width = input('Please enter filter width: ');
if rem(width,2) == 0
disp('ERROR! Width must be odd. The program will continue with the inputted width plus 1.')
width = width + 1;
end
Smoothed = CTask2p1_f(x,width);
figure
plot(t,x,'bo',t,Smoothed,'r')
xlabel('Index')
ylabel('Data Value')
title('Smoothing Filter')
legend('Original Data','Smoothed')
Thanks
Accepted Answer
More Answers (0)
Categories
Find more on Smoothing and Denoising in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!