Even & Odd Discrete time Signals
조회 수: 87 (최근 30일)
이전 댓글 표시
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
답변 (3개)
Hafiz
2024년 6월 9일
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');
댓글 수: 0
Paul
2024년 6월 9일
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)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!