Thursday, 11 April 2019

Matlab FFT on Sinusoidal Code

amp = 3;
freq = 5;
phi = pi/4;
fs = 50;
Ts = 1/fs;
l = 1; %second samples

sineWaveFunc(amp,freq,phi,Ts,l)

plot(t, s)
xlabel('time')
ylabel('Amplitude')
title('sineWave')

myPow = calcPower(fs, freq, s);

myFFT = findFFT(s);
%calculate power

N = length(s) + 1;  % to be even :)
S = fft(s,N);
SdB = 20*log10(S);  % or SdB = mag2db(S);
freqs = (0:N/2-1)*fs/N;

figure(2)
plot(freqs, SdB(1:N/2))
xlabel('frequency')
ylabel('amplitude')
title('spectrum')

function sinFFT = findFFT(s)
N = length(s) + 1;
sinFFT = fft(s, N);
%plot(sinFFT, N)
end

function pow = calcPower(fs, freq,s)
Nsamp = round(fs/freq);

pow = (1/Nsamp)*sum(abs(s(1:Nsamp)).^2);
end

function [s , t] = sineWaveFunc(amp,freq,phi,Ts,l)
t = 0:Ts:l;
s = amp*sin(2*pi*freq*t + phi);
end

No comments:

Post a Comment

Diaries of an Aspiring Astrophysicist (DAS Astro) Podcast

Diaries of an Aspiring Astrophysicist Episode 1: The last year has been weird Episode 2: Cosmic Collisions and Gravitational Wa...