I'm getting this error as shown below
이전 댓글 표시
The code
function [Xk] = DFT(xn)
N = length(xn); %frame size
Xk = zeros(1,N); %array size xn
%DFT Algorithom
for k = 0:(N-1)
n = 0:(N-1);
Xk(k+1) = sum(xn(n+1).*exp((-1i*2*pi*k*n)/N));
end
Xk = abs(Xk);
end
The error
The left-hand side has been constrained to be non-complex, but the right-hand side is complex. To correct this problem, make the right-hand side real using the function REAL, or change the initial assignment to the left-hand side variable to be a complex value using the COMPLEX function.
Please Help
답변 (1개)
Walter Roberson
2022년 8월 2일
function [Xkr] = DFT(xn)
N = length(xn); %frame size
Xk = complex(zeros(1,N)); %array size xn
Xkr = zeros(1,N);
%DFT Algorithom
for k = 0:(N-1)
n = 0:(N-1);
Xk(k+1) = sum(xn(n+1).*exp((-1i*2*pi*k*n)/N));
end
Xkr = abs(Xk);
end
댓글 수: 3
Walter Roberson
2022년 8월 2일
I am not sure why you are taking abs(): discrete fourier transform of real data always returns complex values unless the phases are all 0.
Sean
2022년 8월 2일
Walter Roberson
2022년 8월 2일
I think you should code
function [Xk] = DFT(xn)
N = length(xn); %frame size
Xk = complex(zeros(1,N)); %array size xn
n = 0:(N-1);
%DFT Algorithom
for k = 0:(N-1)
Xk(k+1) = sum(xn(n+1).*exp((-1i*2*pi*k*n)/N));
end
end
but that each place you call this function, you should code as expecting a complex result. Which you can take the abs() or real() or imag() part of, as appropriate.
카테고리
도움말 센터 및 File Exchange에서 C2000 Microcontroller Blockset에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!