Would like help on smoothing data a noisy data would using a central moving average by also using iteration and scalar operations.

12 views (last 30 days)
I'm not quite able to understand what the assignment is asking for. When it comes to "except k = 1 and k = N where x(1) = y(1).
The assignment explains this .. "Write a MATLAB program that will smooth a 1D array of noisy data using an averaging filter. The noisy data is stored in the MATLAB workspace file noisydata.mat. The data is stored in a 1D row array named x. The smoothing filter should process the 1 by N array of noisy data x and produce a 1 by N array of smoothed data y using a central moving average, y(k) = x(k-1) + x(k+1)/2 , except for k = 1 and k = N, where y(1) = x(1) and y(N) = x(N).
Your program
should plot the original noisy data versus the sample number (index) and the smoothed
data versus the sample number on separate axes in separate figure windows.
The program should perform all the operations using Iteration (for loops) and scalar
operations. Do not use element by element comparisons, element by element
mathematical operations, or MATLAB’s built in sum function.
clear; clc;
% load data
load('noisydata.mat','x');
k = 1;
avg = zeros(1,length(x));
% create smoothing filter using a central moving average
for vecVal = x
if k > 1
avg(k) = (x(k-1) + x(k+1))/2;
k = k + 1;
disp(avg)
end
end

Accepted Answer

Image Analyst
Image Analyst on 23 Sep 2015
I assume you can't use conv() either since it says you must use for loops. So you're going to need two for loops, one over elements of x, and one over the window. Here's a start:
for k = 1 : length(x)-N
% Some code to initialize the sum
for wk = 1 : N
% More code to compute the sum
end
% More code to assign the sum to the output
end
  2 Comments
Jason
Jason on 23 Sep 2015
Thank you! So i figured out the smoothing out part. But I don't understand when the instructions say " Your program should plot the original noisy data versus the sample number (index) and the smoothed data versus the sample number on separate axes in separate figure windows. " This is the code I have now I just need a little help with plotting it the way the instructions ask for.
clear;
clc;
% load data
load('noisydata.mat','x');
% allocate space
total = zeros(1, length(x));
% create smoothing filter using a central moving average
for k = 2: length(x)- 1
total(k) = (x(k-1) + x(k+1));
end
y = total/2;
% display output of original noisy data vs. sample number(index)
figure(1)
plot(x)
% display output of smoothed data vs. the sample numver on seperate axis
% in seperate windowss.
figure(2)
plot(y)
Image Analyst
Image Analyst on 23 Sep 2015
Looks like you ignored my answer. You have no loop over the window of N elements. You're just averaging the element to the right and left of the window center - no accommodation for N elements at all.
While I don't think that style of plotting you did will illustrate the smoothing effect in the best way (as compared to in the same plot), it is what the directions say to do.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!