view extra/FFT of a Simple Sinusoid.m @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
line wrap: on
line source
% Example 1: FFT of a DFT_Sinusoid

% Parameters:
N=64;       % Must be a power of two
T=1;        % Set sampling rate to 1
A=1;        % Sinusoidal Amplitude
phi=0;      % Sinusoidal phase
f=0.25;      % Frequency (cycles/sample)
n=[0:N-1];   % Discrete Time axis
x=A*cos(2*pi*n*f*T+phi);    % Sampled sinusoid
X=fft(x);   % Spectrum

% Plot time data:
figure(1);
subplot(3,1,1);
plot(n,x,'*k');
ni= [0:.1:N-1];  % Interpolated time axis
hold on;
plot(ni,A*cos(2*pi*ni*f*T+phi),'-k'); grid off;
title('Sinusoid at 1/4 the Spampling Rate');
xlabel('Time (samples)');
ylabel('Amplitude');
text(-8,1,'a)');
hold off;

% Plot spectral magnitude;
magX= abs(X);
fn=[0:1/N:1-1/N]; %Normalized frequency axis
sublplot(3,1,2);
stem(fn,magX,'ok');grid on;
xlabel('Normalized Frequency(cycles per sample))');
ylabel('Magnitude(Linear)');
text(-.11,40,'b)');

% Same thing on a dB scale:
spec=20*log10(magX);    %Spectral magitude in dB
subplot(3,1,3);
plot(fn,spec,'--ok'); grid on;
axis([0 1 -350 50]);
xlabel('Normalized Frequency (ccles per sample))');
ylabel('Magnitude(dB)');
text('Magnitude(dB)');
text(-.11,50, 'c)');
cmd=['print -deps ', '../eps/example1.eps'];
disp(cmd); eval(cmd);