Create a Matlab function named fourser that is invoked to provide the coefficien
ID: 3349135 • Letter: C
Question
Create a Matlab function named fourser that is invoked to provide the coefficients of the fourier series up to the user-defined Nth value. Specifically, the function header should have a form 1) function [avg,ak,bk,rw,err fourser(t,x,T,N) where t is time, x is the waveform that is being analyzed, T is its fundamental period of the waveform, and N is the number of terms of the fourier series that is desired. ak and bk are the Fourier coeffiecients. rw is the waveform produced by evaluating your N term series, err is the rms error between rw and the x. Note the avg is the average value of x. Prove your function works by inputting different waveforms x, and showing rw and x match well as you increase the number of terms N. 2)Explanation / Answer
function[avg,ak,bk,rw,err] = cal_fs(t,x, T, N)
ck = zeros(1,2*N+1); %intialize a row vector of 2N+1 zeros to store co-efficients of exponential fourier series
ak = zeros(1,2*N+1); %intialize a row vector of 2N+1 zeros to store ak of trignometric fourier series
bk = zeros(1,2*N+1); %intialize a row vector of 2N+1 zeros to store bk of trignometric fourier series
w0 = 2*pi/T; % calculating the value of fundamental frequency from given fundamental period
%% the following loop computes the fourier series coefficients by evaluating the integrals and stores in arrays
for k = -N:N
ck(1,1+k+N) = (1/T) * int(x * exp(-1i*k*w0*t), t, 0, T);
ak(1,1+k+N) = (2/T) * int(x*cos(k*w0*t),t,0,T);
bk(1,1+k+N) = (2/T) * int(x*sin(k*w0*t),t,0,T);
end
% the average value of signal is found by integrating over a single period
avg=1/T * int(x,t,0,T);
%using the summation formula for the fourier series,add the terms to get
%value of rw
for k = -N:N
rw = ak(1,1+k+N)*cos(k*w0*t) + bk*sin(k*w0*t);
end
%% error is calculated as difference between signal and approximation using series
err = x-rw;
end
%%the above function calculates only the Fourier coefficients