# Harmonics in Matlab

T = 3*(1/25);
fs = 1000;
t = 0:1/fs:T-1/fs;
x = sawtooth(2*pi*25*t,1/2);
x = (x + 1)*0.5;
subplot(3,1,1)
plot(t,x)
grid on
xlabel('Time t (sec)')
ylabel('Amplitude')


This is my code and my signal. I want to find its fifth and seventh harmonics. How can I do that ? I cannot find any source or any example for this. Can anybody help me ?

• Harmonics are usually defined for periodic signals. The usual approach is to find the signal's Fourier Series; each term of the series is a harmonic. Can you clarify your definition of harmonic, and why do you need to do this in Matlab? – MBaz Apr 18 at 20:34
• I am trying to learn Matlab. For example, I want to sum first harmonic and fifth harmonic. After that, I will sum first and ninth harmonic and I will compare these two with my original signal. – Oğuzhan Kırlar Apr 18 at 20:39
• I would use Fourier Series to find the harmonics, then Matlab to generate them and add them. – MBaz Apr 18 at 21:20

Your sawtooth waveform is periodic with a time period of $$T = 0.04s$$, therefore basic frequency is $$\frac{1}{T} = 25Hz$$, so to find $$5^{th}$$ and $$7^{th}$$ harmonic, project this signal/correlate the signal over any period T with this harmonic. Let $$tr(t)$$ denote the trinagluar waveform over 1 period. For ex: $$a_5 = \frac{2}{T}\int_{t=0}^T tr(t)Cos(2\pi*5*25t)\,dt$$ similarly $$a_7 = \frac{2}{T}\int_{t=0}^Ttr(t)Cos(2\pi*7*25t)\,dt$$ where $$a_5$$, $$a_7$$ denote the coefficients of the $$5^{th}$$ and $$7^{th}$$ harmonic.
The $$5^{th}$$ harmonic would therefore be $$a_5Cos(2\pi*5*25*t)$$ and $$7^{th}$$ hamonic is $$a_7Cos(2\pi*7*25*t)$$
• f5 = @(t)t.*cos(2.*pi.*5.*25.*t); a5 = integral(f5,0,0.04); a55 = a5.*cos(2.*pi.*5.*25.*t); subplot(3,1,2) plot(t,a55) I write this code and output didn't seem right to me. Am I wrong or is it true ? (I am adding output to the question ) – Oğuzhan Kırlar Apr 19 at 12:18