Fit the sawtooth wave with sin/cos harmonic

2 次查看(过去 30 天)
Fit the sawtooth wave with sin/cos harmonic, and plot the figure with the number of harmonics k=3, 5, 11, 50
We are studying online for school, and I don't quite understand the content and our teacher is no help. Would you please help me.

回答(1 个)

Mathieu NOE
Mathieu NOE 2021-11-19
hello
try this
clc
clearvars
f_sig = 10; % signal freq
periods = 5; % periods
duration = periods*(1/f_sig);
fs = 200*f_sig; % sampling frequency must be x times higher than signal frequency. According to sampling theorem (Shannon's theorem)
% if you want to go as high as the 50th harmonic , then x
% must be at least 2x50 = 100
t = 0:1/fs:duration;
x = 0.5*(1+sawtooth(2*pi*f_sig*t)); % sawtooth signal between 0 and +1
% x = 0.5*(1+square(2*pi*f_sig*t)); % sawtooth signal between 0 and +1
samples = length(x);
% fourier analysis - decompose signal into dc value and cos and sin harmonics
dc_value = mean(x);
harmonics = 50; % harmonics k=3, 5, 11, 50
for k = 1:harmonics
cos_sig = cos(2*pi*k*f_sig*t);
sin_sig = sin(2*pi*k*f_sig*t);
cos_comp(k) = 2/samples*trapz(cos_sig.*x);
sin_comp(k) = 2/samples*trapz(sin_sig.*x);
% create signal from sin and cos terms
new_sig(k,:) = cos_comp(k)*cos_sig + sin_comp(k)*sin_sig;
end
% do sum all harmonics + dc mean value
new_sig = sum(new_sig,1)+dc_value;
plot(t,x,'b',t,new_sig,'r')

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by