Clear Filters
Clear Filters

Even & Odd Discrete time Signals

56 views (last 30 days)
Tauqir Hassan
Tauqir Hassan on 10 Feb 2019
Answered: Paul on 9 Jun 2024
I am writing a program to seperate, plot even and odd parts of a discrete time signal. Following is my code:
I am getting error at line 3 & 4. "Array indices must be positive integers or logical values."
Since I am trying to flip and invert the signal x[n] , so 'x(-n)' is obvious to use but I am getting error. Kindly help.
n= -3:4;
x=[0 0 0 2 3 -1 2 -3];
xe= ( x(n)+x(-n) )/2;
xo= ( x(n)-x(-n) )/2;
subplot(311);stem(n,x);grid on; xlabel('n');ylabel('Amplitude');title('Original Signal');
subplot(312);stem(n,xe);grid on; xlabel('n');ylabel('Amplitude');title('Even Signal');
subplot(313);stem(n,xo);grid on; xlabel('n');ylabel('Amplitude');title('Odd Signal');
  2 Comments
ANAND AMAR
ANAND AMAR on 11 Oct 2020
Error in (line 3)
xe= ( x(n)+x(-n) )/2;

Sign in to comment.

Answers (3)

S. Sukkrishvar Vijay
S. Sukkrishvar Vijay on 31 Jul 2020
n + (-1)^n

Hafiz
Hafiz on 9 Jun 2024
n= -3:4;
x=[0 0 0 2 3 -1 2 -3];
xe= ( x(n)+x(-n) )/2;
xo= ( x(n)-x(-n) )/2;
subplot(311);stem(n,x);grid on; xlabel('n');ylabel('Amplitude');title('Original Signal');
subplot(312);stem(n,xe);grid on; xlabel('n');ylabel('Amplitude');title('Even Signal');
subplot(313);stem(n,xo);grid on; xlabel('n');ylabel('Amplitude');title('Odd Signal');

Paul
Paul on 9 Jun 2024
Matlab doesn't support zero or negtaive indices, so it won't be possible to implement this operation using ordinary vector indexing.
One approach is to define a fucntion that returns elements of the signal x[n] for values of n. One option is to use an anonymous function.
Though not stated expliclty, it appears that x[n] = 0 for n < 0 and x[n] = 0 for n > 4.
x = @(n) (n == 0)*2 + (n == 1)*3 + (n == 2)*-1 + (n == 3)*2 + (n == 4)*-3;
n = -5:5;
tiledlayout(3,1);
nexttile,stem(n,x(n)),title('x[n]')
xe = (x(n) + x(-n))/2;
nexttile,stem(n,xe);title('xe[n]')
xo = (x(n) - x(-n))/2;
nexttile,stem(n,xo);title('xo[n]')
all(x(n) == xe + xo)
ans = logical
1

Categories

Find more on Signal Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!