Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
time<->frequency conversions and displaying in matlab

Subject: time<->frequency conversions and displaying in matlab

From: MICHAEL

Date: 18 Jul, 2009 08:10:04

Message: 1 of 5

hi

i know that to convert to frequency domain i need to do the fft and ifft to convert to time domain.
but how to display it right in matlab? suppose that i generate signal in time/frequency domain and need to convert to frequency/time domain.

thank you

Subject: time<->frequency conversions and displaying in matlab

From: Wayne King

Date: 18 Jul, 2009 10:12:01

Message: 2 of 5

Hi Michael, you need to create time and frequency vectors that are meaningful in the sense that the interval between sequence values is correct. There are a number of examples of this in the Matlab doc, so please do look for these particularly in the Signal Processing Toolbox documentation, but the following example may help:

dt = 1e-3; %Assume a sampling frequency of 1 kHz.
t = 0:dt:1-dt; %1 second time vector in .001 increments. length 1000
x =cos(2*pi*250*t)+0.5*randn(size(t)); %250 Hz sine wave in noise
plot(t,x); %plot time data
xlabel('Seconds');
ylabel('Amplitude');
x_fft=fft(x); %Take the DFT of the signal
df=1/(length(x)*dt); %here is the DF in frequency (increment between DFT bins)
freq =0:df:500; %creating meaningful frequency vector
figure;
plot(freq,20*log10(abs(x_fft(1:length(x)/2+1))));
xlabel('Hz'); ylabel('dB');

Note that to make the above agree with PSD estimates in Matlab, the proper scaling needs to be applied to the squared magnitudes of the DFT.

Hope that helps get you started,
wayne
"michael" <bezenchu@gmail.com> wrote in message <h3s00r$6f9$1@fred.mathworks.com>...
> hi
>
> i know that to convert to frequency domain i need to do the fft and ifft to convert to time domain.
> but how to display it right in matlab? suppose that i generate signal in time/frequency domain and need to convert to frequency/time domain.
>
> thank you

Subject: time<->frequency conversions and displaying in matlab

From: MICHAEL

Date: 18 Jul, 2009 11:41:01

Message: 3 of 5

1. why did you divide by length(x) in df=1/(length(x)*dt);


"Wayne King" <wmkingty@gmail.com> wrote in message <h3s75h$s3$1@fred.mathworks.com>...
> Hi Michael, you need to create time and frequency vectors that are meaningful in the sense that the interval between sequence values is correct. There are a number of examples of this in the Matlab doc, so please do look for these particularly in the Signal Processing Toolbox documentation, but the following example may help:
>
> dt = 1e-3; %Assume a sampling frequency of 1 kHz.
> t = 0:dt:1-dt; %1 second time vector in .001 increments. length 1000
> x =cos(2*pi*250*t)+0.5*randn(size(t)); %250 Hz sine wave in noise
> plot(t,x); %plot time data
> xlabel('Seconds');
> ylabel('Amplitude');
> x_fft=fft(x); %Take the DFT of the signal
> df=1/(length(x)*dt); %here is the DF in frequency (increment between DFT bins)
> freq =0:df:500; %creating meaningful frequency vector
> figure;
> plot(freq,20*log10(abs(x_fft(1:length(x)/2+1))));
> xlabel('Hz'); ylabel('dB');
>
> Note that to make the above agree with PSD estimates in Matlab, the proper scaling needs to be applied to the squared magnitudes of the DFT.
>
> Hope that helps get you started,
> wayne
> "michael" <bezenchu@gmail.com> wrote in message <h3s00r$6f9$1@fred.mathworks.com>...
> > hi
> >
> > i know that to convert to frequency domain i need to do the fft and ifft to convert to time domain.
> > but how to display it right in matlab? suppose that i generate signal in time/frequency domain and need to convert to frequency/time domain.
> >
> > thank you

Subject: time<->frequency conversions and displaying in matlab

From: Wayne King

Date: 18 Jul, 2009 18:07:01

Message: 4 of 5

Hi Michael, because the DFT estimates the frequency content of a discrete-time sequence at intervals (in Hz) of 1/(N*DT) where N is the length of the sequence and DT is the sampling interval.
wayne
"michael" <bezenchu@gmail.com> wrote in message <h3sccd$r6l$1@fred.mathworks.com>...
> 1. why did you divide by length(x) in df=1/(length(x)*dt);
>
>
> "Wayne King" <wmkingty@gmail.com> wrote in message <h3s75h$s3$1@fred.mathworks.com>...
> > Hi Michael, you need to create time and frequency vectors that are meaningful in the sense that the interval between sequence values is correct. There are a number of examples of this in the Matlab doc, so please do look for these particularly in the Signal Processing Toolbox documentation, but the following example may help:
> >
> > dt = 1e-3; %Assume a sampling frequency of 1 kHz.
> > t = 0:dt:1-dt; %1 second time vector in .001 increments. length 1000
> > x =cos(2*pi*250*t)+0.5*randn(size(t)); %250 Hz sine wave in noise
> > plot(t,x); %plot time data
> > xlabel('Seconds');
> > ylabel('Amplitude');
> > x_fft=fft(x); %Take the DFT of the signal
> > df=1/(length(x)*dt); %here is the DF in frequency (increment between DFT bins)
> > freq =0:df:500; %creating meaningful frequency vector
> > figure;
> > plot(freq,20*log10(abs(x_fft(1:length(x)/2+1))));
> > xlabel('Hz'); ylabel('dB');
> >
> > Note that to make the above agree with PSD estimates in Matlab, the proper scaling needs to be applied to the squared magnitudes of the DFT.
> >
> > Hope that helps get you started,
> > wayne
> > "michael" <bezenchu@gmail.com> wrote in message <h3s00r$6f9$1@fred.mathworks.com>...
> > > hi
> > >
> > > i know that to convert to frequency domain i need to do the fft and ifft to convert to time domain.
> > > but how to display it right in matlab? suppose that i generate signal in time/frequency domain and need to convert to frequency/time domain.
> > >
> > > thank you

Subject: time<->frequency conversions and displaying in matlab

From: Greg

Date: 19 Jul, 2009 00:50:47

Message: 5 of 5

On Jul 18, 4:10 am, "michael" <bezen...@gmail.com> wrote:
> hi
>
> i know that to convert to frequency domain i need to do the fft and ifft to convert to time domain.
> but how to display it right in matlab? suppose that i generate signal in time/frequency domain and need to convert to frequency/time domain.

dt = 1/Fs, T = N*dt
t = dt*(0:N-1);
t = 0: dt: T-dt;

df = 1/T, Fs = N*df
f = df*(0:N-1);
f = 0: df: Fs-df;

X = fft(x)/N; % Notice the scaling by N
absX = abs(X);

xr = N*ifft(X); % 'r' indicates reconstructed

figure(1), hold on
plot(t,x,'-.'),
plot(t,xr,'ro')

Q = ceil((N+1)/2) % No of unique points for real x
%
% For x real and k > Q, X(k) = conj( X(N+2-k) )
%
P = floor((N+1)/2)
fQ = (Q-1)*df
fQ = df* ceil((N-1)/2
fP = (P-1)*df
fP = df*floor((N-1)/2)
fb = f-fQ;
fb = -fQ:df:fP;
fb = df*[ -ceil((N-1)/2) : floor((N-1)/2) ]

% N even
%
% fbe = df*[ -N/2 : N/2-1 ];
% fbe = [ -Fs/2 : df : Fs/2-df ];
%
% N odd
%
% fbo = df*[ -(N-1)/2 : (N-1)/2 ];
% fbo = [ -(Fs-df)/2 : df : (Fs-df)/2 ];

figure(2)
plot(fb,fftshift(absX),'-.')

Hope this helps

Greg

Tags for this Thread

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.

Contact us