Search code examples
matlab

Why am I getting wrong signs in the odd indexes of my Fourier coefficients?


I've been trying to calculate step by step the DFS of some signals in a single period, first by calculating the coefficients and then calculating the entire series.

With each step I plot in a graph what I've been doing in order to see it visually. This is my coefficient calc function:

function [Ak, k] = FourierCoeffGen(x, n, N_per)
    W0 = (2*pi/N_per); 
    k = -N_per/2:N_per/2-1; % Index range for the Fourier coefficients
    Ak = (1/N_per)*x*exp(-1*j*W0*(n.')*k); 
end

Now after getting my vector of coefficients I try to reconstruct the original signal using:

function x = DiscreteFourierSeries(Ak, k, N_per)
    W0 = (2*pi/N_per);
    expo = exp(1j*W0*(k.')*k);
    x = Ak*(expo.');
end

After doing all of those I plot the 3 graphs (original signal, coefficients and the reconstructed signal) in graphs and try to understand what I got:

% Example 2 - x[n] = 1 for -5 < n < 5, 0 elsewhere
N_per = 20;
n2 = -N_per/2:N_per/2-1;
%x2 = (n2 > -5) & (n2 < 5);
x2 = zeros(1,20);
x2(7:15) = 1;


% Plot original function 2
subplot(3, 1, 1);
stem(n2, x2);
xlabel('n');
ylabel('x[n]');
title('Original Function 2');


% Calculate Fourier coefficients for Example 3
[Ak2, k2] = FourierCoeffGen(x2, n2, N_per);

%Plot coefficients for function 2
subplot(3, 1, 2);
plot(n2, Ak2);
xlabel('n');
ylabel('A[k]');
title('X[n] Coefficients');

% Reconstruct signal for Example 3
x2_reconstructed = DiscreteFourierSeries(Ak2, k2, N_per);

% Plot reconstructed function 2
subplot(3, 1, 3);
stem(n2, x2_reconstructed);
xlabel('n');
ylabel('x_{reconstructed}[n]');
title('Reconstructed Function 2');

sgtitle('Original Functions vs Reconstructed Functions');

My issue is that the coefficients that I get using this simple discrete dirac step function (-1 for -5<n<5 and 0 otherwise) with a period of 20 - all have the wrong sign in the odd indexes of the output coefficient vector.

I honestly tried debugging it, trying to refresh my math knowledge but I cannot find a decent reason for is it happening:

Odd indexes have the wrong sign

Would appreciate some help

I have tried switching up the input and playing with it, but it seemed to be pointless. The math should be ok, I looked up all the formulas (please correct me if I'm wrong).

EDIT - I have tried starting my indexing to be from 0 to 20 instead of -10 to 10 (even though both should work fine as it is a periodic function), and yet the odd signed indexes still seem to be with the wrong sign (notice here how X=1 should be Y=-0.315688) enter image description here


Solution

  • The following is a solution to exercise 5.2 in

    book Digital Signal Processing in MATLAB

    2nd edition

    authors: Vinay K. Ingle, John G. Proakis

    % example 5.2 periodic square pulses
    % time resolution: N and L, increase it to improve also 
    % frequency resolution
    % Xt=dft2(xn,N) is like [Xt,k]=dft()  but allows to play with
    
    L=15;N=50;n=[0:1:N-1]
    xn=[ones(1,L),zeros(1,N-L)]
    M=100
    [Xt,k]=dft2(xn,N,M)
    % k=[-N/2:N/2]
    magXt=abs(Xt);phXt=angle(Xt)*180/pi
    stem(k,magXt)
    
    % zei=find(magXt<0.00001);phXt(zei)=zeros(1,length(zei))
    
    subplot(3,1,1);H_s1=stem(n,xn,'filled');set(H_s1,'markersize',3)
    % title('1 cycle of x(n)','fontsize',10)
    % ylabel('amplitude');ntick=[n3(1):5:n3(N3),N3]';axis([-1 N3 -6 6])
    % set(gca,'XTickMode','manual','XTick',ntick,'fontsize',8)
    
    subplot(3,1,2);H_s2=stem(k,magXt,'filled');set(H_s2,'markersize',3)
    % axis([-1,N3,min(magXt3),max(magXt3)])
    % title('|Xt3(k)|','fontsize',10);ylabel('magnitude')
    ktick=[k(1):5:k(N)]';set(gca,'XTickMode','manual','XTick',ktick)
    
    subplot(3,1,3);H_s3=stem(k,phXt,'filled');set(H_s3,'markersize',3)
    % title('angle(Xt3(k))','fontsize',10);xlabel('k');ylabel('degree')
    
    ktick=[k(1):5:k(N),N]';axis([-1 N -180 180])
    % set(gca,'XTickMode','manual','XTick',ktick)
    % set(gca,'YTickMode','manual','YTick',[-180 -90 90 180])
    

    enter image description here

    This solution does not show the odd coefficients deviations that somewhere in DiscreteFourierSeries and FourierCoeffGen functions seem to be introduced.

    The support function I use is

    % Discrete Fourier Transform Series coefficients
    function [Xk,k]=dfs(xn,N)
    n=[0:1:N-1]
    k=[0:1:N-1]
    WN=exp(-1j*2*pi/N)
    nk=n'*k
    WNnk=WN.*(-nk)
    Xk=xn*WNnk
    

    Hope it helps.