Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Smoothing filter A smoothing filter averages out rapid changes from a data se

ID: 3743570 • Letter: 1

Question

1. Smoothing filter A smoothing filter averages out rapid changes from a data set and is typically used to remove high-frequency fluctuations (e.g., measurement noise) from a signal or to reduce the amount of intensity variation between one pixel and the next one in images In this task, you will design a smoothing filter by writing a custom function with the declaration: smoothed CTask2p1 f (x, width) and save it in a file named CTask2p1 f.m. The filter should take a vector of noisy data (x) and smooth it by doing a symmetric moving average with a window of the specified width. For points near the beginning and the end of the data set, use a smaller number of samples on either side of the samples in the average calculation, but be sure to keep an equal number of samples on either side of the sample under test. For example: If width-5 and length (x) -100 Then for n-3:98, smoothed (n) -mean (x (n-2:n+2)) for n98 Note: You must write your own code to implement the smoothing algorithm rather than use any built-in MATLAB functions such as smooth. Your code should also meet the following requirements 1) 2) The lengths of x and smoothed should be equal. For symmetry to work, make sure that width is odd. If it isn't, increase it by 1 to make it odd and display a warning in command window, but still do the smoothing 3) You can use a loop and mean (which should be easy but may be slow), or more efficiently by using conv (if you are familiar withconvolution)

Explanation / Answer

clear all
close all
clc


t=linspace(0,1,100); % Given indices for data points
noise=rand(1,length(t)); % random noise
x=cos(2*pi*t) + 0.5*(rand(size(noise))-0.5); % dataset x

width=5; % width of the filter
smoothed5=CTask2pl(x,width); % call filetr to smooth data

width=20; % width of the filter
smoothed20=CTask2pl(x,width); % call filetr to smooth data

plot(t,x,'b*'); % plot original noisy points
hold on
plot(t,smoothed5,'r-'); % plot smoothed points
legend('Width=5')
title('Smoothing Filter');
xlabel('Index');
ylabel('Data Value')

figure
plot(t,x,'b*'); % plot original noisy points
hold on
plot(t,smoothed20,'k-'); % plot smoothed points
legend('Width=20')
title('Smoothing Filter');
xlabel('Index');
ylabel('Data Value')


function smoothed=CTask2pl(x,width)

% check the filer width
if mod(length(width),2)==0
warning('Width of the filter should be odd. Resetting to w=w+1. ');
width=width+1;
end

start=(width+1)/2; % find the intial point where complete filter will be used

for k=1:length(x)
if k<start % if working on pixel less than the mid of the given filetr, reduce filter length
smoothed(k)=mean(x(1:k+(k-1)));
  
elseif k>length(x)-start % if working on pixel less than the mid of the given filetr, reduce filter length
smoothed(k)=mean(x(k-(length(x)-k):end));
  
else % normal filetr operation
smoothed(k)=mean(x(k-2:k+2));
end
end
end