plot the Fourier-transform of vogeltje
1 view (last 30 days)
Ashutosh Singh Baghel on 14 Apr 2022
I understand you wish to plot fourier transform using function implementation and then calling function over some inputs.
You can start by writing a simple function for some simple expression, for eg 'sum of two numbers'.
Simply uncomment the code snippet below to see the output of the code.
% % define Inputs
% A = 4;
% B = 5;
% % call function 'mysum' over the two input arguments
% out = mysum(A,B)
% % define function 'mysum'
% function out = mysum(A,B)
% out = A + B;
You can find more details on how to write functions in the link from Mathworks Documentation.
For fourier transform, you can start by plotting fft of a deterministic signal, for eg 'sin' function;
% k, power for number of samples
% changing vale of k will increase/decrease number of sample points due to
% which width of peak is controlled
k = 8;
% N, number of grid points (preferably N=2^k for some integer k)
N = 2^k;
% Ts, scalar of Sample time =
Ts = 1/200;
% t, vector of equidistant sampled time
t = linspace(0,N*Ts,N);
% f, vector of sampled function values 'f(t)'
f = sin(2*pi*20*t);
% % N, number of grid points (preferably N=2^k for some integer k)
[fshift,yshift] = fouriertransform(f,Ts);
% Plot fshift and yshift
% define function for fourier transform
function [fshift,yshift] = fouriertransform(f,Ts)
% fourier transform of f(t)
F = fft(f);
% Sampling frequency
fs = 1/Ts;
% length of input signal
n = length(f);
% row vector of sampled frequencies (0:(N/2-1))*2*pi/T/N
fshift = (-n/2:n/2-1)*(fs/n);
% row vector of sampled Fourier transform 'F(w)'
yshift = fftshift(F);
Local functions must always be defined at the bottom of the code.