Even & Odd Discrete time Signals
Show older comments
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');
1 Comment
ANAND AMAR
on 11 Oct 2020
Error in (line 3)
xe= ( x(n)+x(-n) )/2;
Answers (4)
S. Sukkrishvar Vijay
on 31 Jul 2020
0 votes
n + (-1)^n
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');
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)
Tannu
on 24 Feb 2026
Edited: Walter Roberson
on 24 Feb 2026
clc;
clear all;
close all;
n = [-5,-4,-3,-2,-1,0,1,2,3,4,5];
x1 = [1,2,3,4,5,6,5,4,3,2,1];
x2 = [-5,-4,-3,-2,-1,0,1,2,3,4,5];
x3 = [3,1,4,2,6,5,7,0,2,1,4];
signals = {x1, x2, x3};
N = length(n);
mid = (N+1)/2;
for k = 1:3
x = signals{k};
Even_flag = 1;
Odd_flag = 1;
% Check Even and Odd separately
for i = 1:N
if x(i) ~= x(N-i+1)
Even_flag = 0;
break;
end
end
for i = 1:N
if x(i) ~= -x(N-i+1)
Odd_flag = 0;
break;
end
end
% For odd signal, middle value must be 0
if Odd_flag == 1
if x(mid) ~= 0
Odd_flag = 0;
end
end
% Display result
if Even_flag == 1
fprintf('Signal %d is Even\n', k);
elseif Odd_flag == 1
fprintf('Signal %d is Odd\n', k);
else
fprintf('Signal %d is Neither Even nor Odd\n', k);
end
end
```
Categories
Find more on Signal Generation and Preprocessing 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!