## Perform an Online Harmonic Analysis Using the SimscapeSpectrum Analyzer Block

### Harmonic Distortion

Nonlinear loads create power distortion in the form of harmonics, that is, voltages and currents that are multiples of the fundamental frequency. Harmonic waveforms can result in energy losses through heat dissipation and in reduced power quality. They can also cause equipment to malfunction or to become damaged. Standards development organizations such as the Institute of Electrical and Electronics Engineers (IEEE) and the International Electrotechnical Commission (IEC) define the recommended limits for harmonic content in electric power systems.

This example shows how to examine harmonic distortion in your model using offline, that is after simulation, and online, that is during simulation, analyses. The offline analysis uses the Simscape™ Electrical™ harmonic analysis functions and helps you to determine configuration settings for, and verify the results of, the online analysis. The online analysis uses the Simscape Spectrum Analyzer block.

### Prerequisite

This example requires a simulation log variable in your MATLAB® workspace. The model in this example is configured to log Simscape data for the whole model for the entire simulation time.

To learn how to determine if a model is configured to log simulation data, see Examine the Simulation Data Logging Configuration of a Model.

### Perform an Offline Harmonic Analysis

1. Open the model. At the MATLAB command prompt, enter:

```model = 'ee_composite_rectifier'; open_system(model)```

The example model contains a three-phase rectifier. The model also contains a Selector block that outputs only the a-phase from three-phase current signal that it receives from the PS-Simulink Converter block.

2. Simulate the model.

`sim(model)`

3. View the time-domain results. Open the Scope block.

The time domain analysis shows that the rectifier is converting the voltage, but it does not include any information about the frequencies in the signal.

4. Determine configuration settings and calculate the expected results for an online harmonic analysis. Perform an offline harmonic analysis.

1. The Simscape Electrical harmonic analysis functions require that you use a fixed-step solver. Determine the solver type and sample time for the model. To turn on sample-time highlighting, in the Simulink® editor menu bar, select Debug > Information Overlays > Sample Time > Colors.

The model is running at a discrete rate, therefore it is using a fixed-step solver, with a sample time of 1e-4 s.

2. Use the `ee_getHarmonics` function to calculate the harmonic order, the harmonic magnitude, and the fundamental frequency based on the voltage source currents.

```[harmonicOrder,harmonicMagnitude,fundamentalFrequency] = ... ee_getHarmonics(simlog_ee_composite_rectifier.Voltage_Source.I);```

3. Performing an online harmonic analysis using the Spectrum Analyzer block requires that you specify a value for maximum harmonic order and the resolution bandwidth (RBW). The RBW depends on the fundamental frequency.

Extract and display the maximum harmonic order and the fundamental frequency:

```disp(['Maximum Harmonic Order = ', num2str(max(harmonicOrder))]) disp(['Fundamental Frequency = ', num2str(fundamentalFrequency)])```
```Maximum Harmonic Order = 30 Fundamental Frequency = 60```

4. Determine the peak value of the fundamental frequency. This value is useful for filtering out negligible harmonics and for verifying the results of the offline analyses.

```fundamentalPeak = harmonicMagnitude(harmonicOrder==1); disp(['Peak value of fundamental = ', num2str(fundamentalPeak),' A']);```
`Peak value of fundamental = 1945.806 A`

5. Filter out small harmonics by identifying and keeping harmonics that are greater than one thousandth of the fundamental peak frequency.

```threshold = fundamentalPeak ./ 1e3; aboveThresold = harmonicMagnitude > threshold; harmonicOrder = harmonicOrder(aboveThresold)'; harmonicMagnitude = harmonicMagnitude(aboveThresold)';```

6. Display the harmonic data in a MATLAB table.

```harmonicRms = harmonicMagnitude./sqrt(2); harmonicPct = 100.*harmonicMagnitude./harmonicMagnitude(harmonicOrder == 1); harmonicTable = table(harmonicOrder,... harmonicMagnitude,... harmonicRms,... harmonicPct,... 'VariableNames',{'Order','Magnitude','RMS','Percentage'}); display(harmonicTable);```
```harmonicTable = 10×4 table Order Magnitude RMS Percentage _____ _________ ______ __________ 1 1945.8 1375.9 100 5 218.86 154.75 11.248 7 105.83 74.835 5.439 11 85.135 60.2 4.3753 13 57.599 40.729 2.9602 17 50.417 35.65 2.5911 19 37.612 26.596 1.933 23 33.859 23.942 1.7401 25 26.507 18.743 1.3622 29 23.979 16.955 1.2323 ```

7. Calculate the total harmonic distortion (THD) percentage using the `ee_calculate_ThdPercent` function.

```thdPercent = ee_calculateThdPercent(harmonicOrder,harmonicMagnitude); disp(['Total Harmonic Distortion Percentage = ' num2str(thdPercent),' %']);```
`Total Harmonic Distortion percentage = 14.1721 %`

### Perform an Online Harmonic Analysis

1. In the Simulink editor that contains the `ee_composite_rectifier` model, replace the Scope block with a Spectrum Analyzer block from the Simscape Utilities Library:

1. Delete the Scope block.

2. Left-click within the block diagram.

3. After the search icon appears, type `spec`, and then from the list, select the `Spectrum Analyzer` from the `Utilities` library.

4. Connect the Spectrum Analyzer block to the output signal from the Subsystem i.

2. Configure the Spectrum Analyzer block using the Spectrum Settings panel.

1. Open the Spectrum Analyzer.

2. Open the Spectrum Settings panel. On the Spectrum Analyzer toolbar, click the button.

3. Configure the parameters on the Main Options pane.

1. Configure the block to display the root mean square (RMS) of the frequency. From the Type dropdown menu, select `RMS`.

2. Determine the value to specify for the resolution bandwidth (RBW) using this equation:

`$RBW=\frac{NENBW*f}{N},$`

where,

• NENBW is the normalized effective noise bandwidth, a factor of the windowing method used. The Hanning (Hann) window has an NENBW value of approximately 1.5.

• f is the fundamental frequency.

• N is the number of periods.

• RBW is the resolution bandwidth in Hz.

For a fundamental frequency of 60 Hz over 10 periods, using a Hann window,

`$RBW=\frac{1.5*60Hz}{10}=9Hz$`

For RBW (Hz), specify `9`.

4. Expand the Windows Options pane and specify an Overlap (%) of `90`.

5. Specify the maximum number of peaks for the analyzer to display. In the menu bar, select Tools > Measurements > Peak Finder. Alternatively, in the Spectrum Analyzer toolbar, select the Peak Finder button. In the Peakfinder pane, in the Settings section, for Max Num of Peaks, enter `30`. This value is based on the maximum harmonic order as indicated by the offline analysis.

6. Set the number of harmonics to use for measuring harmonic distortion. Specify a number that captures the largest harmonic order that the offline analysis captures. In the menu bar, select Tools > Measurements > Distortion Measurements. Alternatively, in the Scope toolbar, click the Distortion Measurements button. Scroll as required to see the Distortion Measurements pane.

In the Distortion Measurements pane, for Num Harmonics, again enter `30`.

3. Simulate the model.

`sim(model)`

The THD percentage is 14.17% and the fundamental peak power is 1375.89 Vrms at 0.06 kHz (60 Hz). These results agree with the results from the offline harmonic analysis.