# Optimum System for Diagnosing Power Quality in Electrical Microgrids
## 2 Materials and Methods
### Methodology for Harmonic Flow Assessment  
After voltage and current measurements are properly collected, these data can be utilized for PQ analysis. Depending on the application, specific procedures and algorithms are implemented to achieve the desired objectives. In this study, the data are employed to identify distortion sources in an experimental microgrid, serving as the selected use case to demonstrate the effectiveness of the DiagElect system. To this end, a procedure that incorporates the fast Fourier transform (FFT) algorithm and the Harmonic Global Index (HGI) is used. The procedure for this study is described in detail below. Each SDMD within the microgrid captures data spanning 10 cycles. For precise analysis, it is essential to compare these cycles and identify one that is unaffected by any transient disturbances. The data from the selected cycle are then used to compute the spectrum for each phase of voltage and current. The FFT is employed for this analysis, as it effectively decomposes the time-domain signal into its frequency components. Using FFT, the amplitudes and phases of each frequency component are extracted to determine the RMS values of harmonic voltage \(V_{\text{h}}\) and current \(I_{\text{h}}\) for each harmonic order \(\text{h}\). The resulting spectrum enables the calculation of voltage and current THD using (1) and (2):  
\[V_{\text{THD}}=\sqrt{\sum_{\text{h}=2}^{50}(\left\|V_{\text{h}}\right\|/\left\| V_{\text{1}}\right\|)^{2}} \tag{1}\]  
\[I_{\text{THD}}=\sqrt{\sum_{\text{h}=2}^{50}(\left\|I_{\text{h}}\right\|/\left\| I_{\text{1}}\right\|)^{2}} \tag{2}\]  
where \(\left\|V_{\text{h}}\right\|\) and \(\left\|I_{\text{h}}\right\|\) are the norms of the RMS values of the hth harmonic voltage and current, respectively, and \(\left\|V_{\text{1}}\right\|\) and \(\left\|I_{\text{1}}\right\|\) are the norms of the RMS value of fundamental voltage and current, respectively. The total and fundamental components of active and reactive power are derived from (3) to (6):  
\[P_{\text{T}}=\sum_{\text{h}=1}^{50}\left\|V_{\text{h}}\right\|\times\left\| I_{\text{h}}\right\|\times\cos\left(\varphi_{\text{V}_{\text{h}}}-\varphi_{\text{h}} \right) \tag{3}\]  
\[P_{\text{1}}=\left\|V_{\text{1}}\right\|\times\left\|I_{\text{1}}\right\| \times\cos\left(\varphi_{\text{V}_{\text{1}}}-\varphi_{\text{h}_{\text{1}}}\right) \tag{4}\]\[Q_{T}=\sum_{h=1}^{50}\|V_{h}\|\times\|I_{h}\|\times\text{sen}\big{(}\varphi_{V_{h}}- \varphi_{I_{h}}\big{)} \tag{5}\]  
\[Q_{1}=\|V_{1}\|\times\|I_{1}\|\times\text{sen}\big{(}\varphi_{V_{1}}-\varphi_{I_{ 1}}\big{)} \tag{6}\]  
where \(P_{T}\) and \(P_{1}\) are the total and fundamental components of active power, \(Q_{T}\) and \(Q_{1}\) are the total and fundamental components of reactive power, and \(\varphi\) represents the phase angle of the hth harmonic voltage and current. To identify the nodes that inject harmonics into the microgrid, the HGI [42] is utilized, which is a widely adopted index over the past decades [13; 43]. The calculation procedure, detailed in [42], involves classifying each harmonic current based on the sign of its corresponding active power. Specifically, if the active power of the hth harmonic is negative, the hth harmonic current \(I_{h}\) flows from the load to the source, forming part of the load current component \(I_{l}\). Conversely, if the power is positive, the hth harmonic current \(I_{h}\) flows from the source to the load, forming part of the source component \(I_{s}\). As in (7):  
\[ \begin{cases}I_{h}=I_{h}\text{ if }P_{h}<0;\text{ otherwise }I_{h}=0\\ I_{s_{h}}=I_{h}\text{ if }P_{h}>0;\text{ otherwise }I_{s_{h}}=0\end{cases}  \tag{7}\]  
where \(I_{h}\) is the hth harmonic of the load current component and \(I_{s_{h}}\) is the hth harmonic of the source current component. The HGI value is then calculated using (8):  
\[HGI=\sqrt{\sum_{h=2}^{50}\big{\|}I_{h}\big{\|}^{2}/\sqrt{\sum_{h=2}^{50}\big{\|} I_{s_{h}}\big{\|}^{2}}} \tag{8}\]  
A node is considered a distortion source if the HGI value is significantly above zero.