DSPRelated.com
Forums

Fwd: Need help on FFT error msg

Started by Sameer Herlekar April 17, 2008
Hello all,

Just thought I'll post to the group a one-on-one correspondence email
from a few weeks ago, for the group members'
interest/thoughts/discussions

Thanks
Sameer

---------- Forwarded message ----------
From: Sameer Herlekar
Date: Tue, Mar 18, 2008 at 11:41 PM
Subject: Re: [matlab] Need help on FFT error msg
To: Haizad Mohamad
Haizad,

When you're doing the FFT, you generally want to keep the FFT length
the same as the length of the time vector 'n', in this case N/2. That
is accomplished by the line Fdt(f,N/2);

Running the instruction

plot(abs(Fd))

will then plot the (N/2 length) vector of the FFT result. In this plot
the point N/2 corresponds to sampling frequency 'H' while half the FFT
length (equal to N/4) corresponds to (H/2) Hz which is the largest
frequency which can be uniquely represented, according to the Nyquist
sampling theorem. The region N/4 to N/2 in the plot are the negative
frequencies which are identical in magnitude to the +ve frequencies
and are symmetric with the +ve frequencies about the point N/4. i.e.,
MATLAB plots +ve frequencies from elements 1 to N/4 and -ve
frequencies from N/4 to N/2 if the FFT is of length N/2. For
illustration purposes these -ve frequencies can be discarded, which is
what I did by displaying only the first N/4 elements of the FFT vector
(since only these N/4 elements represent unique frequencies).

To plot versus rad/sec just just mulitply the time vector by 2pi as follows:

plot((n(1:N/4)/(N/4))*(H/2)*2*pi, abs(Fd(1:N/4)))
xlabel('Frequency (rad/sec)'); ylabel('Magnitude')

HTH and good luck,
Sameer
On Tue, Mar 18, 2008 at 7:35 AM, Haizad Mohamad
wrote:
>
>
>
> Hi Sameer,
>
>
>
> Thanks for the great help!
>
> Got a few questions though.
>
> when you do the plot function, you used N/4 in the parameters. What
>
> exactly is that for?
>
> Also, your xlabel is in Frequency (Hz), if I want the plotted graph
>
> to be in rad/sec, what must I amend in the programme?
>
>
>
> Really appreciate your time and help.
>
>
>
> Thanks again!
>
>
>
>
>
> ----- Original Message ----
> From: Sameer Herlekar
> To: h...@yahoo.com.sg
> Cc: m...
> Sent: Tuesday, 18 March 2008 3:05:12
> Subject: Re: [matlab] Need help on FFT error msg
>
> To plot any kind of graph you need every x-axis value to possess a
> corresponding y-axis value. The error is because your y-axis argument
> in the command 'plot' attempts to access 512 elements of Fd when there
> are only 11 elements in Fd. If you replace the line Fdt(f);
>
> with the instruction
> Fdt(f,N/2);
>
> you will no longer have the error. But I don't think that is what
> you're looking for. You can just use ordinary sampling theory which
> requires that you replace the parameter 't' in the signal 'f' with the
> parameter n/H. You can compute an FFT of length equal to the length of
> the vector 'n'. Keep in mind that a sampling frequency of H Hz implies
> that only frequencies upto H/2 can be represented uniquely. Hope the
> following code works for you:
>
> H=5;
> N24;
> n=[0:N/2-1];
> f*sin(1.5*(n/H))+5*sin(2.5*(n/H))+2.5*sin(5*(n/H))+1.25*sin(10*(n/H));
> Fdt(f,N/2);
> plot((n(1:N/4)/(N/4))*(H/2), abs(Fd(1:N/4)))
> xlabel('Frequency (Hz)'); ylabel('Magnitude')
>
>
> On Mon, Mar 17, 2008 at 7:48 AM, wrote:
> >
> >
> >
> >
> >
> >
> > Hi everyone
> >
> > Tried to write the program for the following question and got the error
> > msg. Appreciate anyone's help please! Thanks.
> >
> > QUESTION
> > A signal is represented by the expression:
> >
> > f(t) = 10 sin (1.5t) + 5 sin (2.5t) + 2.5 sin (5t) + 1.25 sin (10t)
> >
> > Use MATLAB to obtain a spectrum (magnitude only) of the signal
> > reconstructed from the sampled signal if the sampling frequency is:
> >
> > a) 5 Hz
> >
> > PROGRAM
> > >> H=5;
> > >> N24;
> > >> n=[0:N/2-1];
> > >> t=0:10;
> > >> w=2*pi*H/N*n;
> > >> f*sin(1.5*t)+5*sin(2.5*t)+2.5*sin(5*t)+1.25*sin(10*t);
> > >> Fdt(f);
> > >> plot(w,abs(Fd(1:N/2))/H);
> >
> > ??? Index exceeds matrix dimensions.
> >
> >