# Harmonics Analysis in Matlab: Full guide

## Fast Fourier Transform (FFT) Analysis in MATLAB/Simulink

Contents :

In this tutorial, we will present a harmonics analysis of a simple circuit in Matlab/Simulink using FFT (Fast Fourier Transform).

FFT converts a time-domain waveform to a frequency domain and by this transformation, all signal frequency components can be obtained easily.

This means that different harmonics of a current or voltage waveform can be derived using this technique. let’s get into the details!

## Circuit description and implementation in Simulink

Figure 1 shows the circuit implemented in Simulink which consists of a rectifier with a resistive load. The system has the following elements:

- Three-phase source created by “
*AC Voltage Source*” block, - a three-phase full-bridge diode-based rectifier which is created by “
*Universal Bridge*” block , - A resistive load which forms with the rectifier a non-linear load.

**Parameters:**

- The source RMS voltage is 220V/phase in this circuit, and
- The load resistance is 150 Ohm.
- The sampling time in this simulation is Ts=1μs which is enough for this simulation.

## Harmonics analysis of load current

In this example, we would like to **check harmonic content in phase A current**. The latter should be firstly measured and saved for analysis.

Current is measured by the block “*current measurement*” and it is visualized and saved using the “scope” block as shown above.

Running the above Simulink file for 0.1s, we get the following input current:

It is evident that the** signal is not purely sinusoidal**, and harmonics are available in this signal. Now, the harmonics and their values should be derived. There are two ways for this purpose that are discussed in this tutorial. The methods are:

**FFT analysis using powergui block****Exporting signal values to workspace and use FFT script**

**The input current harmonics in a three-phase full-bridge rectifier is 6k±1 where k=0, 1, 2, etc.**

Therefore, it can be expected that the mentioned harmonics would be available in the FFT results.

## FFT analysis using powergui block: Method 1

### Step 1: save signal in “*structure with time*” format

Firstly, the current signal should be saved for FFT analysis. This can be achieved by the mean of “*scope*” block or to “*Workspace*” block.

- Double click on the
*scope*block > then click on*configuration properties*(scope settings), we get the following window:

- The signal would be named and saved by going to “
*Logging*” (“*history*“) tab.

- You can specify the Variable name in the corresponding field, in this case, we choose “
*Input_Current*“

- The “
*Format*” field is for defining the saved signal format in the workspace, which can be:*Structure with time**Structure**Array**Dataset*

As shown in the above illustration, the** “ Structure With Time” format is chosen which is required for powergui FFT tool**.

### Step 2: open FFT tool

To start the analysis, the FFT Analysis must be selected in powergui block according to the following figure.

- Double click on “
*FFT Analysis”*yields the following window:

### Step 3: choose signal and set FFT settings

We distinguish four main parts:

**Available signals**: which is related to choosing a waveform. As can be seen, the variable name of “*Input_Current*” is available in “*Name*” field. The “Display” field also lets the user see the entire signal or the FFT window in*Signal part*.**Signal part**: it plots the chosen signal entirely or part of it (FFT window).**FFT analysis**: in this part, harmonics analysis is displayed according to the parameters chosen in*FFT settings*.**FFT settings**: is the most important part of FFT Analysis.: allows us to choose at which time we would like to analyse the signal, generally we start after the transient state and analyse it it in steady state. There is no transient part, so the starting time can be at the beginning of the signal (0 s).**Start time field**: as it names states, it corresponds of how many cycles we would like to analyze. We can go for one cycle for FFT calculation.**Number of cycles****Fundamental frequency**: in this case study, the frequency is 50 Hz, and the signal is periodical.- The “
**Max frequency**” field, the user can use the highest frequency that is necessary. In this study, 20th harmonic or 1000 Hz is enough. For noisy signals, high-order harmonics are available, and, in some cases, the user wants to derive high-frequency components. - For the result presentation, there are
**six choices that users can select based on the report that is needed**

### Step 4: Display results

As an example, “Bar (relative to fundamental)” has been chosen, and the bar chart is available below after clicking on the **Display button**.

- It is clear that the
**harmonic magnitudes decrease regarding higher-order harmonics**. - Moreover, the above rule 6k±1 harmonics are available in the input current of a three-phase full-bridge rectifier circuit (1st harmonic, 5th harmonic, 7th harmonic, 11th harmonic, 17th harmonic and 17h harmonic).

The above is also confirmed by **choosing List instead of Bar**:

## FFT analysis in Matlab Workspace: Method 2

In the previous section, a powergui block was used for the FFT analysis, and different harmonics were presented.

“powergui” is not the only method to find the harmonics of a signal. **Harmonics can be obtained by writing codes in an m-file environment** as follows.

### Step 1: save signal in “*array*” format

First, the **signal must be saved to the workspace in an “Array” format** as shown in the next figure. In this array, time and signal magnitude have been saved, and these parameters must be separated for signal processing.

### Step 2: use FFT script to analyse your signal

The harmonic values can be calculated by writing a short script for FFT analysis. In this script, the **FFT function should be used to derive the values of the harmonics of the input current**.

As can be seen in the following script,

- the sampling time and frequency are defined firstly.
- Then, time and signal magnitude are saved in two variables, namely “time” and “mag”.
- At the final step, the FFT function is used to determine the harmonic values based on the fundamental harmonic.

```
Ts = 1e-6; %Sampling Time
Fs = 1/Ts; %Sampling Frequency
F1 = 50; %Fundamental Frequency
%Simulated Signal Data
time = Input_Current(:,1);
mag = Input_Current(:,2);
subplot(2,1,1)
plot(time,mag)
%FFT Calculation
L = length(mag);
Y = fft(mag,L);
Yabs = abs(Y);
f = 0:(1/time(end))/F1:(Fs/2-(1/time(end)))/F1; % Frequency based on Nyquist Theorem
Ymax = max(Yabs); %Fundamental Magnitude
subplot(2,1,2)
bar(f,Yabs(1:length(f))/Ymax) %Plotting bar chart
xlim([0,20]) %X-axis Limitation
```

Compiling this code, we get the harmonic orders and their values, which are similar to the previous results of the FFT analysis.

## Harmonics content of rectifier supplying a resistive laod

From above, the harmonic content of the electrical circuit corresponding to a rectifier feeding a resistive load is reported in the following table:

Harmonic order | Harmonic % |
---|---|

1st harmonic (fundamental frequency) | Used as a reference |

5th harmonic (250Hz) | 22.57% |

7th harmonic (350Hz) | 11.29% |

11th harmonic (550Hz) | 9.03% |

13th harmonic (650Hz) | 6.45% |

17th harmonic (850Hz) | 5.64% |

19th harmonic (950Hz) | 4.52% |

## Conclusion

All in all, MATLAB suggests various methods for harmonics study, and users can check the FFT results in Simulink or m-file. Having the discretized signal in the workspace environment would be a great feature. The users can apply various signal processing approaches by writing different scripts and plotting them in an appropriate shape.