Question: 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

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 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).

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:

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.

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 with convolution).

Write a new script called CTask2p1.m to test your function.

t = linspace(0,1,100);

noise = rand(1,length(t));

x = cos(2*pi*t) + 0.5*(rand(size(noise))-0.5);

These commands create a noisy data set stored in the variable x.

Write additional codes in this script to use your filter function with a width=5 to smooth the noisy data set and plot both the original noisy data and your smoothed data. Then, use a width=20 to smooth the same set of noisy data and comment the difference of the smoothed data. The result may look something like this:

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Given the task at hand we are asked to create a custom function in MATLAB that implements a symmetric moving average smoothing filter Lets break this ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Document Format (2 attachments)

PDF file Icon

60933089ae9b8_23510.pdf

180 KBs PDF File

Word file Icon

60933089ae9b8_23510.docx

120 KBs Word File

Students Have Also Explored These Related Programming Questions!