Solved

# matlab- amplitude/phase spectra and Fourier transform

Posted on 2006-04-26
6,165 Views
I had two signal one is periodic and the other is aperiodic.for example

period like--> waveformPeriodic = [t1,sin(t1/10)];
aperiodic like--> waveformAperiodic = [sort(rand(100,1)*100),rand(100,1)];

How can I generate the amplitude/phase spectra and Fourier transform for them??
0
Question by:hsuyf

LVL 2

Accepted Solution

Hello hsuyf,

Lets just take waveformPeriodic as our example.

Save the code at the end of this message to a file called "fftden.m" somewhere on your matlab search path, then issue the following command to see an fft plot of your data :
>> fftden(waveformPeriodic(:,1),waveformPeriodic(:,2));

Issue the following slightly different command to return amplitude and phase spectra instead of generating a plot :
>> [freq,density,phaseDeg] = fftden(waveformPeriodic(:,1),waveformPeriodic(:,2));

Gordon

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
CUT AND PASTE THE FOLLOWING CODE INTO A FILE CALLED fftden.m
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

function [freq,density,phaseDeg] = fftden(tIn,xIn,sampleFreq)
% [freq,density,phaseDeg] = fftden(t,x,sampleFreq)
% generate a power spectral density from a time domain signal using fft
% density should scale with neither sampleFreq nor length of xIn
%
% if no outputs are requested then a plot is generated
% an example is given is the function is executed with no inputs

if nargin>0
if nargin < 3 || isempty(sampleFreq)
sampleFreq = 1/min(diff(tIn));
end

t       = min(tIn):1/sampleFreq:max(tIn);
x       = interp1(tIn,xIn,t);

n       = 2^(nextpow2(length(x)));    % use next highest power of 2 greater than or equal to length(x)
fftOut  = fft(x,n);                   % take fft, padding with zeros so that length(fftOut) is equal to n
fftMag   = fftOut.*conj(fftOut);
warning off MATLAB:divideByZero
fftPhase = 180*atan(imag(fftOut)./real(fftOut))./pi;
warning on MATLAB:divideByZero

i       = 1:(1+n/2);
freq    = (i-1)*sampleFreq/n;
density = fftMag(i)/(n*sampleFreq);
phaseDeg= fftPhase(i);

if nargout==0
figure
plot(freq,density,'b', freq,density,'b.');
xlabel('Frequency (Hz)');
ylabel('Power Spectral Density');
grid on
zoom on
end
else
if nargin>0
error('Can''t generate outputs without inputs.  Use "fftden;" to see example.')
end
figure
oi = 0.001;  %1024 2048 4096 8192
t=0:1/1024:1;    [f,d]=fftden(t,sin(2*pi*t*200),1000);    c='b';   o=oi*0; plot(f,d+o,c, f,d+o,[c,'.']);    hold on
t=0:1/1024:1;    [f,d]=fftden(t,sin(2*pi*t*200),2000);    c='g';   o=oi*1; plot(f,d+o,c, f,d+o,[c,'.']);
t=0:1/1024:1;    [f,d]=fftden(t,sin(2*pi*t*200),4000);    c='r';   o=oi*2; plot(f,d+o,c, f,d+o,[c,'.']);
t=0:1/1024:1;    [f,d]=fftden(t,sin(2*pi*t*200),8192);    c='m';   o=oi*3; plot(f,d+o,c, f,d+o,[c,'.']);
t=0:1/1024:2;    [f,d]=fftden(t,sin(2*pi*t*200),8192);    c='k';   o=oi*4; plot(f,d+o,c, f,d+o,[c,'.']);
t=0:1/1024:3;    [f,d]=fftden(t,sin(2*pi*t*200),8192);    c='c';   o=oi*5; plot(f,d+o,c, f,d+o,[c,'.']);
t=0:1/1024:4;    [f,d]=fftden(t,sin(2*pi*t*200),8192);    c='k';   o=oi*6; plot(f,d+o,c, f,d+o,[c,'.']);
t=0:1/1024:5;    [f,d]=fftden(t,sin(2*pi*t*200),8192);    c='c';   o=oi*7; plot(f,d+o,c, f,d+o,[c,'.']);

title('Example set of Frequency Distributions');
xlabel('Frequency (Hz)');
ylabel('Power Spectral Density');
grid on
zoom on
end
0

Author Comment

thanks Gordon
Hi Gordon

I think I will pick 2 wave file for my aperiodic signal and periodic signal. So the presentation would like below, how can I generate the amplitude spectra, phase spectra, and FT. since i am very new to matlab i think it looks like easier for me... could you please help me about this. thank you in advance...

subplot(2,2,1), plot(aper), title('Entire waveform of aperiodic signal');
smallRange = 100000:100000+floor(f/100);

subplot(2,2,2), plot(per), title('Entire waveform of periodic signal');
smallRange = 100000:100000+floor(fc/100);

subplot(2,2,3), plot(?????????), title('phase spectra of aperiod signal');  -------> how can I plot "aperiod" signal'samplitude spectra, phase spectra, and FT here&#65311;

subplot(2,2,4), plot(???????), title('phase spectra of aperiod signal');    -------->how can I plot "period" signal'samplitude spectra, phase spectra, and FT here&#65311;
0

LVL 2

Expert Comment

Hello hsuyf,

The answer I gave you already contains examples for how to achieve this.

Thanks,
Gordon
0

LVL 2

Expert Comment

Hello hsuyf,

I need to earn more points to get to Premium Membership.

If you think the answer I gave you was worth points (and hopefully you do because there's alot there) please could you accept it ?

Thanks,
Gordon
0

## Featured Post

### Suggested Solutions

How to Win a Jar of Candy Corn: A Scientific Approach! I love mathematics. If you love mathematics also, you may enjoy this tip on how to use math to win your own jar of candy corn and to impress your friends. As I said, I love math, but I gu…
Have you ever thought of installing a power system that generates solar electricity to power your house? Some may say yes, while others may tell me no. But have you noticed that people around you are now considering installing such systems in their …
how to add IIS SMTP to handle application/Scanner relays into office 365.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…