I am looking for help writing a Matlab function that takes in the input which is
ID: 3738044 • Letter: I
Question
I am looking for help writing a Matlab function that takes in the input which is the xp(t) , the order of Fourier series and t and gives out the outputs defined below
function [ak,bk,f0,Series] = FourierSeries(input,order,t)
where input is the xp(t), our input signal, order is the order of Fourier Series you hope to use, and t is the corresponding time, ak and bk are the ak and bk coefficients, f0 is the fundamental frequency in Hz, and Series is the Fourier Series representation of the signal xp(t).
Explanation / Answer
SAVE THE FOLLOWING CODES IN MATLAB AND GET THE RESULTS-
SAVE THE FOLLOWING FUNCTION FILE IN MATLAB-
function [freq,amp,phase,dc]=fourier_series_real(t,x)
rel_tol = 1e-4; % relative tolerance, to determine trailing zero truncation
if (~isreal(x))
clc;
beep;
disp('fourier_series_real Error: x(t) must be real.');
dc = NaN; amp = NaN; freq = NaN; phase = NaN;
return;
end
t = t-t(1); % shifting time to zero.
T = t(end); % period time.
N = 100; % number of samples
if (mod(N,2) == 1)
N = N + 1;
end
N = N/2;
ok = 0;
while (~ok)
N = N*2; % increase number of samples
if (N > 10e6)
clc;
beep;
disp('fourier_series_real Error: signal bandwidth seems too high.');
disp('Try decreasing the sample time in the input time vector t,');
disp('or increasing the relative tolerance rel_tol');
dc = NaN; amp = NaN; freq = NaN; phase = NaN;
return;
end
dt = T/N;
t1 = 0:dt:(T-dt);
x1 = interp1(t,x,t1,'cubic',0);
xk = (1/N)*fft(x1);
dc = abs(xk(1));
xkpos = xk(2:(N/2));
xkneg = xk(end:-1:(N/2+2));
freq = [1:length(xkpos)]/T; % Hz
amp = 2*abs(xkpos);
phase = angle(xkpos);
Am = max(amp);
ii = find((amp(end-10:end)/Am)>rel_tol);
ok = isempty(ii);
end
Am = max(amp);
ii = length(amp);
while (amp(ii) < Am*rel_tol)
ii = ii - 1;
end
amp = amp(1:ii);
freq = freq(1:ii);
phase = phase(1:ii);
end
SAVE THE FOLLOWING SCRIPT FILE IN MATLAB-
T=0:5:pi;
X=sin(t);
[freq,amp,phase,dc]=fourier_series_real(t,x)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.