Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: DFT
Date: Tue, 3 Nov 2009 22:48:01 +0000 (UTC)
Organization: The MathWorks Inc
Lines: 44
Message-ID: <hcqbv1$kve$1@fred.mathworks.com>
References: <hcq6eq$855$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1257288481 21486 172.30.248.38 (3 Nov 2009 22:48:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 3 Nov 2009 22:48:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1597503
Xref: news.mathworks.com comp.soft-sys.matlab:582200


"Madhumitha Iyer" <pyarsa_madhu@yahoo.co.in> wrote in message <hcq6eq$855$1@fred.mathworks.com>...
> x=[zeros(1,10),ones(1,15),zeros(1,10)];    %square wave generator
> %The total number of samples are 10+15+10=35
> figure(1)
> subplot(2,1,1)
> plot(x)
> N=35;
> for k=0:N-1
>     for n=0:N-1
>         x(k)=sum(x(n)*(exp(-j*2*pi*n*k/N)));
>         subplot(2,1,2)
> plot(x(k))
>     end
> end
> 
> The above code is for finding the DFT coefficients.I am getting the following error while running this program:
> 
> "Subscript indices must either be real positive integers or logicals."
> 
> Error in ==> DSPnov3 at 9
>         x(k)=sum(x(n)*(exp(-j*2*pi*n*k/N)));
> Pls tell me how I can rectify the error.I am writing the code for finding DFTs without using the matlab command 'fft'

Hi, how about simplifying a bit and removing one of your for loops.

x=[zeros(1,10),ones(1,15),zeros(1,10)]; %square wave generator
%The total number of samples are 10+15+10=35
subplot(2,1,1)
plot(x)
N=35;
for k=1:N
    freq = (-1i*2*pi*(k-1))/N;
    Wnk=exp(freq.*(0:N-1));
    X(k)=sum(x.*Wnk);
end
subplot(2,1,2);
plot(abs(X))

% you can compare the result against fft() to see they match
figure;
plot(abs(fft(x)));

Hope that helps,
Wayne