The N-Point Moving Average (MA) Filter

39 views (last 30 days)
Kutlu Yigitturk
Kutlu Yigitturk on 22 Apr 2021
Answered: vidyesh on 21 Feb 2024
- Write a Matlab code to load the input signal x[n],
clc
clear all
close all
N = input('Enter Order of Filter: ');
n = 0:N-1;
for k=n
y(k)=(1/N)*(sum(x(k:-1:k-N+1)))
end
plot(y(n))
I have such a solution for step 1, but I get an error. 'Unrecognized function or variable' x '.'
clc
clear all
close all
N = input('Enter Order of Filter: ');
n = 0:N-1;
x = 5;
for k=n
y(k)=(1/N)*(sum(x(k:-1:k-N+1)))
end
plot(y(n))
I get this error when I enter any value for X. 'Array indices must be positive integers or logical values.' How can I fix this problem?
- Then the reduce the noise of this signal using the given MA filter.
I do not know how to reduce the noise, I could not find a useful resource.
- Finally plot the original signal and the filtered one in the same figure.

Answers (1)

vidyesh
vidyesh on 21 Feb 2024
Hello,
I see that you're working on implementing an N-point moving average filter in MATLAB and are encountering a couple of issues. Let's address these errors step by step:
The error 'Unrecognized function or variable x' is due to x not being defined before it's used in the loop. In MATLAB, you need to define all the variables before you can use them. Ensure that x is an existing array with your input signal data before the loop starts.
Regarding the error stating 'Array indices must be positive integers or logical values', note that MATLAB indexing begins at 1. When assigning values to y(k), it is crucial to ensure that the indices used to access elements of x fall within the permissible range. This means indices should be greater than or equal to 1 and less than or equal to the length of the array x. Any attempt to access x using an index outside this range will result in an error.
For more information on arrays, refer the below documentation page:
Hope this helps.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!