Thread Subject: DFT

Subject: DFT

From: Madhumitha Iyer

Date: 3 Nov, 2009 21:14:02

Message: 1 of 7

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'

Subject: DFT

From: TideMan

Date: 3 Nov, 2009 21:36:53

Message: 2 of 7

On Nov 4, 10:14 am, "Madhumitha Iyer" <pyarsa_ma...@yahoo.co.in>
wrote:
> 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'

Well, the error message says it all really:
"Subscript indices must either be real positive integers or logicals."

In other words, you cannot have zero as an index into a vector as you
have done.
You need to start at 1, not zero.
Then, in your summation you must use n-1 and k-1

Subject: DFT

From: Madhumitha Iyer

Date: 3 Nov, 2009 22:08:00

Message: 3 of 7

Thank you for the reply but I am still getting the same error after making the summation limits as k-1 and n-1.I have to get a DFT plot which is a combination of sine waves.Pls help
Thanx.

TideMan <mulgor@gmail.com> wrote in message <26b9039c-8b85-4c0b-b0de-f5050f962223@f18g2000prf.googlegroups.com>...
> On Nov 4, 10:14?am, "Madhumitha Iyer" <pyarsa_ma...@yahoo.co.in>
> wrote:
> > 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'
>
> Well, the error message says it all really:
> "Subscript indices must either be real positive integers or logicals."
>
> In other words, you cannot have zero as an index into a vector as you
> have done.
> You need to start at 1, not zero.
> Then, in your summation you must use n-1 and k-1

Subject: DFT

From: Wayne King

Date: 3 Nov, 2009 22:48:01

Message: 4 of 7

"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

Subject: DFT

From: TideMan

Date: 3 Nov, 2009 23:50:13

Message: 5 of 7

On Nov 4, 11:08 am, "Madhumitha Iyer" <pyarsa_ma...@yahoo.co.in>
wrote:
> Thank you for the reply but I am still getting the same error after making the summation limits as k-1 and n-1.I have to get a DFT plot which is a combination of sine waves.Pls help
> Thanx.
>
> TideMan <mul...@gmail.com> wrote in message <26b9039c-8b85-4c0b-b0de-f5050f962...@f18g2000prf.googlegroups.com>...
> > On Nov 4, 10:14?am, "Madhumitha Iyer" <pyarsa_ma...@yahoo.co.in>
> > wrote:
> > > 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'
>
> > Well, the error message says it all really:
> > "Subscript indices must either be real positive integers or logicals."
>
> > In other words, you cannot have zero as an index into a vector as you
> > have done.
> > You need to start at 1, not zero.
> > Then, in your summation you must use n-1 and k-1

No, No, No, No!!
I guess you have a problem listening.
Matlab has told you and I have re-iterated that you cannot have zero
as an index. Yet you persist.
It will only work when you pay attention to what Matlab is telling
you.
Start your bloody loops with 1, not zero!

Subject: DFT

From: Madhumitha Iyer

Date: 4 Nov, 2009 01:07:02

Message: 6 of 7

Thanx the code worked fine with your suggestion.Thank you once again

"Wayne King" <wmkingty@gmail.com> wrote in message <hcqbv1$kve$1@fred.mathworks.com>...
> "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

Subject: DFT

From: Greg Heath

Date: 5 Nov, 2009 03:04:21

Message: 7 of 7

On Nov 3, 8:07 pm, "Madhumitha Iyer" <pyarsa_ma...@yahoo.co.in> wrote:
> Thanx the code worked fine with your suggestion.Thank you once again
>
>
>
> "Wayne King" <wmkin...@gmail.com> wrote in message <hcqbv1$kv...@fred.mathworks.com>...
> > "Madhumitha Iyer" <pyarsa_ma...@yahoo.co.in> wrote in message <hcq6eq$85...@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

The DFT calculation can be simplified further using no loops.
Just matrix multiplication.

Hope this helps.

Greg

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
subscript indic... astro mmi 3 Nov, 2009 16:19:08
rssFeed for this Thread

Contact us at files@mathworks.com