MATLAB: Filtered signal plot not showing up

moving_average

I'm attempting to filter a certain portion of data from an external file using a moving average for a hw problem, and having trouble figuring out exactly what is wrong. I have no errors when I run the code, but my second plot doesn't show up.
clear all
load raw_data.mat
n = 200;
filter(500:700) = signal(500:700);
for k = 500:(n-1)
mean_w_curr = (signal(k-2) + signal(k-1) + signal(k) + signal(k+1) + signal(k+2))/5
mean_wo_curr = (signal(k-2) + signal(k-1) + signal(k+1) + signal(k+2))/4
if abs(mean_w_curr(k) - mean_wo_curr(k)) > .001
filter(k) = mean_wo_curr(k)
else
filter(k) = signal(k)
end
end
plot(500:700, signal(500:700),'r')
hold on
plot(filter(k),'b')

Best Answer

  • It’s necessary for you to understand the problem with your indexing.
    This:
    n = 200;
    k = 500:(n-1)
    produces:
    k =
    1×0 empty double row vector
    Since this is homework, I’m porviding you with this link to the documentation on the colon operator to understand the reaons this is not working.