 Anonymous - 2 years ago
824 0

No description

MATLAB

# Homework, week 1, task 3

``````N = 200; %sampling rate
Fs = 1/N; %Time step
T = 1; %period
L = N*T %Interval length
t = 0:Fs:T-Fs; %Time vector

x = zeros(1,L); %Building sawtooth
for i=1:L;
x(i) = t(i);
while x(i)>1;
x(i) = x(i)-1;
end
end

f = fft(x)/L; %Making the fft and normalizing it
f = f(1:L/2+1); %Getting rid of the 2nd half as we have a real signal
f(2:end) = f(2:end)*2;
P2 = abs(f); %Single sided amplitude spectrum
Ph = angle(f); %Single sided phase spectrum

X = zeros(1,L); %Building fourier series representation by a function defined at the end.
for i=1:L;
X(i) = FourierTrig(f, 50, (i-1)*Fs, 1); %(Fourier coefficients, number of harmonics used, time step, frequency of sawtooth)
end
%Used the fft fourier coefficients, 50 harmonics
plot(t,X,t, x), title('Fourier Series representation of sawtooth function')
legend('FS with 50 harmonics', 'Sawtooth function')
xlabel('time [s]')
ylabel('Y')
f2 = N*(0:(L/2))/L; %Frequency axis for amplitude spectrum
%%Phase and Amplitude spectrum plots
% stem(f2,P2), title('Amplitude Spectrum')
% xlabel('Frequency, [Hz]')
% ylabel('Amplitude')
%
% stem(f2,Ph), title('Phase Spectrum')
% xlabel('Frequency, [Hz]')

%Creating table with first 10 harmonics
Table = zeros(10,3);
Table(1:10,1) = 1:1:10;
Table(1:10,2) = P2(1:10);
Table(1:10,3) = Ph(1:10);
Table
function Y = FourierTrig(ff, i, t, f);
Y1 = ff(1);
A1 = 0;
B1 = 0;
for m = 1:i;
a = real(ff(m+1));
b = -imag(ff(m+1));
A1 = a * cos(m*2*pi*f*t);
B1 = b * sin(m*2*pi*f*t);
Y1 = Y1+A1+B1;
end
Y = Y1;
end``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download