Not sure how to do a smoothing average of noisy data

Not sure how to do a smoothing average of noisy data based off what the instructions asked this is what I've come up with so far any help would be appreciated.
clear;
clc;
load('noisydata.mat','x');
N = x;
y = zeros(1,length(N));
for k = 2:1:length(x)
if (k~= 1 & k~= N)
y(k) = (x(k-1)+x(k+1))/2;
end
end

 Accepted Answer

You forgot to set y(1) = x(1) before the loop, and, after the loop, set y(length(x)) = x(end). Then have your loop go from k = 2 : (length(x)-1).

3 Comments

This didnt change anything unless I misunderstood what you were saying. Matlab is beating me down today...
clear;
clc;
load('noisydata.mat','x');
N = x;
y = zeros(1,length(N-1));
y(1) = x(1);
for k = 2:1:(length(x)-1);
y(k) = (x(k-1)+x(k+1))/2;
y(length(x))= x(end);
end
figure(1)
plot(N,y)
xlabel('noisydata')
ylabel('samp_num_ind')
figure(2)
plot(y(k))
xlabel('smoothedData')
ylabel('sampleNum')
Well I said to put y(length(x))= x(end); after the loop, not inside it.
And for some reason you plotted only a single point
plot(y(k))
instead of the whole curve:
plot(x, 'r*-');
hold on;
plot(y, 'b-', 'LineWidth', 2);
But anyway, try that, because other than that it looks like it should work. But you didn't attach noisydata.mat so I can't test it.
sorry about that I thought I did just attach it to this comment. I really appreciate the help to! thank you. The reason I plotted only one point was I because I wasn't sure what to compare the noisy data with, and what to compare the smoothed data with.

Sign in to comment.

More Answers (1)

Jason - I think that you have correctly interpreted what the question is asking (less the plot) but you may want to reconsider the condition for your if statement
if (k~= 1 & k~= N)
Remember that k is a scalar but N is a vector/array so this comparison is not what you want. If you want to exclude the first and last elements from x then just extend what you have started with the for statement
for k = 2:1:length(x)-1
So now k ranges from 2 to one less than the length of x which is exactly which values you need to consider (and so no longer have any need for the condition).

3 Comments

This is what I've got and the first part seems to work now but I dont understant what the question asks when it says to plot the original noisy data vs. "The sample number(index)", vice verse plot in a second window smoothed data vs. sample number". am I missing something here?
clear; clc;
load('noisydata.mat','x');
N = x;
y = zeros(1,length(N));
for k = 2:1:length(x)-1
y(k) = (x(k-1)+x(k+1))/2;
end
figure(1)
plot(N,y)
xlabel('noisydata')
ylabel('samp_num_ind')
figure(2)
plot(y(k))
xlabel('smoothedData')
ylabel('sampleNum')
Jason - you are being asked to plot the original noisy data versus the sample number. The sample numbers are the indices into your noisy matrix x. How would you plot this?
I'm not sure this whole problem has beat me down. I know its probably something simple

Sign in to comment.

Asked:

on 19 Feb 2015

Commented:

on 19 Feb 2015

Community Treasure Hunt

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

Start Hunting!