Can you hel me with my code

here is what i have
rng(123);
N=111;
noise= -1+2.*rand(N,1);
for n=1:N
y(n) = sin(2*pi.*n./20) + 2*cos(2*pi.*n./60)+noise(n);
end
figure (1), plot(y),
which is working... now I have to take the answer of this and put it through a few functions to make it smoother. function has a few functions inside, but when i run it it does not work can some one please check it for me and if possible fix it
thank you
function y=LPF(inputData)
x=1:inputData;
for n=1:length(inputData)
if n==1
y(1)=(x(1)+x(2))/2; %for the first value of n
elseif n==inputData %for last value of n
y(inputData)=y(inputData-1)+y(inputData)/2;
elseif n==2 %for 2nd value
y(2)=(x(1)+x(2)+x(3))/3;
elseif n==inputData-1 %for the next to last value
y(inputData-1)=(x(inputData-2)+x(inputData-1)+x(inputData))/3;
else
y(n)=(x(n-2)+x(n-1)+x(n)+x(n+1)+x(n+2))/5;%for all other values of n
end
y=[y,x];
end

4 comentarios

Walter Roberson
Walter Roberson el 30 de Nov. de 2011
Please expand on "when i run it it does not work". Do you mean you encounter an error message, or it does not give the output you would like?
Slo
Slo el 30 de Nov. de 2011
I am sorry it gives me error msg
??? Attempted to access x(3); index out of bounds because numel(x)=2.
Error in ==> LPF at 9
y(n)=(x(1)+x(2)+x(3))/3;
Andrei Bobrov
Andrei Bobrov el 30 de Nov. de 2011
in your case should be inputData >= 5
Slo
Slo el 30 de Nov. de 2011
I got it now thank you

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 30 de Nov. de 2011

0 votos

For your code to work, inputData must be a positive integer scalar which 2 or greater.
Your code would run in to the error message you noted in the circumstance that inputData was a vector whose first element was greater than or equal to 2 but less than three, or in the circumstance that inputData was a scalar whose value was greater than (but not equal to) 2 and less than three, or in the circumstance that inputData was a vector whose first element had a value greater than (but not equal to) 2 and less than three.
If your intended input to the routine is the "y" that you calculated in your first routine, then your line
x=1:inputData;
should then instead be
x = inputData;
and every reference to inputData in the loop should be replaced with length(inputData) such as changing
elseif n==inputData
to
elseif n == length(inputData)

Etiquetas

Aún no se han introducido etiquetas.

Preguntada:

Slo
el 30 de Nov. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by