# Implementation of Fourier Series up to "n" Harmonics in MATLAB

In mathematics, the Fourier series up to "n" harmonics is a technique used to approximate a periodic function through the sum of sine and cosine functions. We can implement the Fourier series up to "n" harmonics in MATLAB programming. Before discussing this implementation, let us first get an overview of Fourier series up to "n" harmonics.

## What is Fourier Series up to "n" Harmonics?

In mathematics, the Fourier series is a method of expressing a periodic function as a sum of sinusoidal functions (sine and cosine functions).

The Fourier series up to "n" harmonics is a way of approximating a periodic function as a sum of sinusoidal signals, where "n" is the number of harmonics or terms included in the expression of the Fourier series.

Mathematically, the Fourier series up to "n" harmonics is expressed as follows −

$$\mathrm{x(t) \: = \: \frac{a_{0}}{2} \: + \: \sum_{k=1}^{n}[a_{k} \cos(2\pi k\: ft)\: + \: b_{k}\sin(2\pi k\: ft)]}$$

Here, x(t) is the given periodic function, f is the fundamental frequency of the function x(t), $\mathrm{a_{0}}$, $\mathrm{a_{k}}$, and $\mathrm{b_{k}}$ are Fourier series coefficients.

In this expression, "n" denotes the number of harmonics or terms in series. We can add more harmonics to the series by increasing the value of "n".

This is all about basics of Fourier series up to "n" harmonics. Let us now discuss how to implement it in MATLAB.

## How to Implement Fourier Series up to "n" Harmonics in MATLAB

In MATLAB, it is a very simple process to implement the Fourier series up to "n" harmonics.

Here is the step-by-step process to implement the Fourier series up to "n" harmonics in MATLAB −

• Step 1 − Define an input function whose Fourier series up to "n" harmonics is to be calculated.

• Step 2 − Calculate the Fourier series coefficients, $\mathrm{a_{0}}$, $\mathrm{a_{n}}$, and $\mathrm{b_{n}}$.

• Step 3 − Calculate the Fourier series up to "n" harmonics. For this, we can use a loop mechanism.

• Step 4 − Plot the original signal and its Fourier series for visual comparison.

### Example

Let us understand the implementation of Fourier series up to "n" harmonics in MATLAB with the help of an example program.

% MATLAB code to implement Fourier series up to "n" harmonics
% Define sample single parameters
T = 1;	% Period of the input signal
f = 1/T;	% Fundamental frequency of input signal
n = 7;	% Number of harmonics

% Create a time vector for the signal
t = 0:0.001:5*T;

% Define a function whose Fourier series is to be found
x = @(t) 0.6 + 0.4*cos(2*pi*1/T*t) + 0.2*sin(2*pi*2/T*t) + 0.3*cos(2*pi*4/T*t);

% Calculate the Fourier series coefficients
a0 = integral(@(t) x(t), 0, T) / T;
an = @(k) integral(@(t) x(t).*cos(2*pi*k*f*t), 0, T) * 2 / T;
bn = @(k) integral(@(t) x(t).*sin(2*pi*k*f*t), 0, T) * 2 / T;

% Calculate the Fourier series up to n harmonics
Fs = a0/2;
for k = 1 : n
Fs = Fs + an(k)*cos(2*pi*k*f*t) + bn(k)*sin(2*pi*k*f*t);
end

% Plot the original signal and its Fourier series up to n harmonics
figure;
plot(t, x(t), 'r', 'LineWidth', 1, 'DisplayName', 'Original Signal');
hold on;

plot(t, Fs, 'g', 'LineWidth', 1, 'DisplayName', 'Fourier Series');

legend('Location', 'best');
xlabel('Time');
ylabel('Amplitude');

title('Fourier Series up to n Harmonics');
grid on;
hold off;


### Output

When you run this code, it will produce the following plot −

### Code Explanation

In this example, we start by defining some signal parameters where, "T" is the time period of the input signal, "f" is the fundamental frequency of the input signal, and "n" is the number harmonics considered in the Fourier series calculation.

Then, we create a time vector from 0 to 5T to plot the signal.

After that we define an input signal "x" whose Fourier series up to "n" harmonics to be calculated.

Next, we calculate the coefficients of the Fourier series, DC component $\mathrm{(a_{0})}$, coefficient of cosine terms $\mathrm{(a_{n})}$, and coefficient of sine terms $\mathrm{(b_{n})}$.

Then, we initialize the Fourier series "Fs" with the DC component. After that we define a loop to add terms of Fourier series to each harmonic up to "n" calculate the Fourier series of the input signal.

Finally, we plot the input signal and its Fourier series up to "n" harmonics.

## Conclusion

In conclusion, Fourier series up to "n" harmonics is a method of representing a periodic signal as a sum of sine and cosine functions with "n" terms. We can use MATLAB programming to calculate the Fourier series of a function up to "n" harmonics.

In this tutorial, I explained the steps involved in the implementation of Fourier series up to "n" harmonics in MATLAB along with an example program. The example program practically demonstrates the process of calculating Fourier series of a periodic function up to "n" harmonics.

Updated on: 25-Oct-2023

97 Views