Hi Lidia,
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;
% end
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
plot(fshift,abs(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);
end
Local functions must always be defined at the bottom of the code.