Algorithm for Assessment of the Switching Angles in the Unipolar SPWM Technique for Single-Phase Inverters

: The main contribution of this paper is to present a simple algorithm that theoretically and numerically assesses the switching angles of an inverter operated with the SPWM technique. This technique is the most widely used for eliminating harmonics in DC-AC converters for powering motors, renewable energy applications, household appliances, etc. Unlike conventional implementations of the SPWM technique based on the analog or digital comparison of a sinusoidal signal with a triangular signal, this paper mathematically performs this comparison. It proposes a simple solution to solve the transcendental equations arising from the mathematical analysis numerically. The technique is validated by calculating the total harmonic distortion (THD) of the generated signal theoretically and numerically, and the results indicate that the calculated angles produce the same distribution of harmonics calculated analytically and numerically. The algorithm is limited to single-phase inverters with unipolar SPWM.


Introduction
Currently, there has been substantial growth in electricity generation through solar energy, which has led to the development of inverters that convert photovoltaic energy generated in direct current (DC) to alternating current (AC) that can be injected into the main power grid [1].On the other hand, there has also been an increase in works on inverters that power the motors of electric vehicles [2].In both cases, the management of the inverter is very relevant, but above all, its modulation techniques have been known since 1973 [3].There are several algorithms to implement in the inverter modulation techniques such as space vector pulse width modulation (SVPWM), selective harmonic elimination pulse width modulation (SHEPWM), sinusoidal pulse width modulation (SPWM), and wavelet modulation [4][5][6][7][8][9][10][11].However, one of the most widely used is SPWM due to the ease of implementation [8,[10][11][12][13][14][15][16][17][18].The SPWM technique can be implemented using analog or digital sampling.Analog implementation is called natural sampling, and digital techniques are called regular sampling [5,6,8,10,15].The analog implementation is very easy, as it only requires comparing a sinusoidal signal with a triangular signal using an op-amplifier, and the resulting signal can be applied to the inverter switches.It is important to clarify that this work is limited to single-phase inverters, which are widely used as microinverters in single-phase systems.But the algorithm shown can be easily scaled to three-phase systems.
There are two cases for the comparison: unipolar and bipolar modulation [3,19].Only the unipolar modulation will be analyzed, since this modulation presents lower THD.The unipolar modulation is shown in Figure 1.
negative half-cycle are obtained by adding 180 degrees (π radians) to the angles obtained in the positive half-cycle.
Among the drawbacks of natural sampling are the generation of sine and triangular signals, noise or interference problems, and the lack of versatility in reconfiguring parameters.Digital implementation solves many of the problems of natural sampling.However, one of the main drawbacks is that evaluating switching angles involves solving transcendental equations that do not have an analytical solution [20].
The main contribution of this work is the proposal of a very simple numerical solution to the transcendental equations.The proposed solution is developed using an algorithm that assess the angles of the SPWM technique.The solution is proposed for unipolar SPWM and single-phase inverters.However, it can be easily extrapolated to three-phase and multi-level inverters.The proposed algorithm can be easily implemented on different platforms such as a microcontroller, a Raspberry Pi or an FPGA.The evaluation of the switching angles can be adjusted for any frequency of the triangular signal allowed by the device on which it will be implemented.The higher the switching frequency of the carrier signal, the greater the displacement of the generated harmonics and the lower the THD that can be achieved.Figure 1 shows the unipolar SPWM technique.A comparison is made between a sinusoidal signal and a triangular signal; the amplitude of the sine signal is A s with a frequency f c = 1/t p , where t p is its period, and the amplitude of the triangular signal is A t .with a frequency f 0 = 1/t 0 where t 0 is its period.For convenience, switching times are expressed in angles rather than in time; both are related by angular frequency ω = 2π f 0 = 2π t 0 .The period of the sinusoidal signal t 0 represents an angle of 2π.When the sine signal is greater than the triangular signal (angle β 1 ), the output of the inverter changes to a "high" state equal to the voltage that feeds the inverter (V DC ), and when the sine signal becomes less than the triangular signal, the output signal of the inverter changes to a "low" state where the output voltage is 0 V (angle β 1f ).The duration of the pulse will be given by subtracting the rising and falling angles (α 1 = β 1f − β 1 ).By symmetry, the angles corresponding to the negative half-cycle are obtained by adding 180 degrees (π radians) to the angles obtained in the positive half-cycle.
Among the drawbacks of natural sampling are the generation of sine and triangular signals, noise or interference problems, and the lack of versatility in reconfiguring parameters.
Digital implementation solves many of the problems of natural sampling.However, one of the main drawbacks is that evaluating switching angles involves solving transcendental equations that do not have an analytical solution [20].
The main contribution of this work is the proposal of a very simple numerical solution to the transcendental equations.The proposed solution is developed using an algorithm that assess the angles of the SPWM technique.The solution is proposed for unipolar SPWM and single-phase inverters.However, it can be easily extrapolated to three-phase and multi-level inverters.The proposed algorithm can be easily implemented on different platforms such as a microcontroller, a Raspberry Pi or an FPGA.The evaluation of the switching angles can be adjusted for any frequency of the triangular signal allowed by the device on which it will be implemented.The higher the switching frequency of the carrier signal, the greater the displacement of the generated harmonics and the lower the THD that can be achieved.
The algorithm was implemented in MATLAB and verified by evaluating the output signal's total harmonic distortion (THD).The THD obtained in the results validated the proposed algorithm; the algorithm and the obtained angles can be digitally implemented in any simple processor like a microcontroller or a Raspberry Pi.
The rest of this article is organized as follows: Section 2 shows the assessment of the SPWM angles, and in Section 3, the results for two cases are presented, one simple case with a unitary output frequency of f o = 1 Hz and a carrier frequency of f c = 12 to obtain p = 5 pulses in a half cycle, and a second more practical case proposed to avoid the presence of harmonics until the harmonic 49 to show the possibility of complying with this standard.In the second case, a unitary frequency f o = 1 Hz and a carrier frequency of f c = 48 to obtain p = 23 pulses in a half cycle were proposed.Section 4 briefly discusses the results, and the conclusions are outlined in Section 5.

Assessment of the SPWM Angles
As shown in Figure 1, the unipolar sinusoidal pulse width modulation (SPWM) technique involves sampling a sinusoidal signal using one positive triangular signal for the positive semi-cycle and one negative triangular signal for the negative semi-cycle of the sinusoidal signal.The sinusoidal signal acts as a reference to obtain the desired sine wave signal, which in many cases is a 60 Hz network signal.The triangular signal, which operates at a considerably higher frequency than the original sine signal, compares and generates pulses that determine the necessary cuts (β and β f ) in the inverter signal.This process is performed by comparing amplitudes between the sine and triangular signals: when the sine signal exceeds the triangular signal, a pulse representing an α angle is generated, indicating the cutoff points in the inverter signal.These cutoffs (β and β f ), calculated by this comparison, allow the resulting square waveform to resemble a sine signal in key areas.In this way, the unipolar SPWM effectively modulates the signal using the high-frequency triangular signals' reference.

Basic Definitions
The SPWM technique requires several important parameters for its correct application.First, we need a sinusoidal signal with an amplitude designated as A s (see Figure 1) and a frequency of usually 60 Hz.
Another parameter is the amplitude of the triangular signal, represented by A t , which is the maximum value reached by this signal.From these definitions, we can calculate the modulation index (M), which is the ratio between the amplitude of the sine signal and the amplitude of the triangular signal, which is expressed by the following equation: One of the key parameters in the SPWM technique is the modulation index (M).It is crucial to keep this value less than 1 to avoid overmodulation, which can negatively affect system performance.Overmodulation can lead to distortion in the output waveform, reducing the efficiency and accuracy of the modulation process.
The ratio between the frequency of the sine signal f o and the frequency of the triangular signal f c , also known as the carrier frequency, is the frequency modulation ratio (m f ) shows in Equation (2).Proper tuning of these frequencies is essential for efficient and accurate modulation.
In addition, it is important to know the number of triangular pulses per half-cycle p t , which the following expression can calculate: Hence, p t represents the number of triangular pulses per half-cycle.Since p t is based on frequency modulation, we can substitute f c / f o and express it as To maintain ¼-wave symmetry, p t must be even Having obtained the number of triangular pulses per half-cycle, it is relevant to calculate the total number of output pulses per half-cycle p, which is obtained by the following expression: This expression indicates that the number of pulses equals the number of triangular pulses minus one.By considering p t to be half of the frequency modulation, we can rewrite the expression as Also, to maintain ¼ wave symmetry, p must be odd.These equations are fundamental to understanding and controlling the comparison process between the triangular signal and the sinusoidal signal in the SPWM technique.
Another condition is to have a unipolar signal for each half-cycle with quarter-wave symmetry with the largest pulse centered at T  4 .If these conditions are satisfied, it is possible to reduce the harmonics.
Meeting these conditions not only reduces unwanted components but also simplifies the process.With this specific quarter-wave symmetry, it is not necessary to calculate all the pulses but only the angles present in a quarter-wave, since these will be replicated throughout the period.
The number of angles required for a quarter-wave is calculated using the following expression: N α is the number of angles for a quarter-wave, and p t is the number of triangular pulses.

Mathematical Definition of the Triangular Signal
In the unipolar SPWM technique, a triangular signal must be compared with a sinusoidal signal to obtain the required angles to form the output pulses.This comparison can be made analogically or digitally; devices are designed for both modes.However, digital evaluation presents the advantage of reconfiguring in real time the parameters of the SPWM, and it is not necessary to generate the triangular and sinusoidal signals, so it is easy to implement the algorithm in a microcontroller or microcomputer like a Raspberry Pi.
Before calculating the required pulses, the triangular signals must be defined mathematically.These signals are represented as two straight lines: one with a positive slope named "y" and one with a negative slope named "y'", and both lines are shown in Figure 2.
As shown in Figure 2, an important aspect is that these lines form a symmetrical triangle, preferably isosceles, with an amplitude A t , and the pulse width of the signal p t is symmetrical on both sides of the triangle.In addition, the positive and negative lines forming the triangular signal must have the same slope, thus ensuring perfect symmetry.
In this context, the midpoint of the pulse width p t will be located at t p 2 , which would be the midpoint of our signal.As shown in Figure 2, an important aspect is that these lines form a symmetrical triangle, preferably isosceles, with an amplitude At, and the pulse width of the signal pt is symmetrical on both sides of the triangle.In addition, the positive and negative lines forming the triangular signal must have the same slope, thus ensuring perfect symmetry.In this context, the midpoint of the pulse width pt will be located at t p 2 , which would be the midpoint of our signal.
Once the amplitude At and the pulse width tp of the triangular signal are defined, we can obtain the slope equation, since straight lines form these signals.The slope is calculated as the ratio of the opposite leg to the adjacent leg in the triangle formed.Since the opposite leg is t p 2 , the slope equation is expressed as where m represents the slope of the positive line; to express the positive line (y) as a function of time t, we can use the following equation: Continuing with the negative line, the slope is negative and is expressed as From the typical expression of one straight line, by clearing b to obtain the ordinate to the origin, we obtain the equation for the negative line (y'): These mathematical expressions represent the positive and negative lines of the triangular signal.However, these equations are only valid for one triangular pulse.Additional equations that consider repeating patterns over time are necessary to deal with a number "nt" of pulses, as shown in Figure 3. Once the amplitude A t and the pulse width t p of the triangular signal are defined, we can obtain the slope equation, since straight lines form these signals.The slope is calculated as the ratio of the opposite leg to the adjacent leg in the triangle formed.Since the opposite leg is t p 2 , the slope equation is expressed as where m represents the slope of the positive line; to express the positive line (y) as a function of time t, we can use the following equation: Continuing with the negative line, the slope is negative and is expressed as From the typical expression of one straight line, by clearing b to obtain the ordinate to the origin, we obtain the equation for the negative line (y'): These mathematical expressions represent the positive and negative lines of the triangular signal.However, these equations are only valid for one triangular pulse.Additional equations that consider repeating patterns over time are necessary to deal with a number "n t " of pulses, as shown in Figure 3.
The equations must be simplified to obtain the expressions for the n t triangle, starting from n t = 0. We start at a time multiple of the triangle pulse width called n t t p where n t represents the n th pulse of a time t p .The end of this n t pulse is calculated by summing the pulse width t p , resulting in end of pulse = t p (n t + 1) (12) The equations must be simplified to obtain the expressions for the nt triangle, starting from nt = 0. We start at a time multiple of the triangle pulse width called nttp where nt represents the nth pulse of a time tp.The end of this nt pulse is calculated by summing the pulse width tp, resulting in end of pulse ( 1) It is important to remember the importance of obtaining the midpoint of the triangular pulse, which is expressed as Once these points are obtained, we define the amplitude At, which remains constant, and the slope, which does not vary.Substituting these new equations into the positive slope in Equation (11), we obtain the expression for the nth positive line y nt : 2 2 2 ( ) The triangular signal is compared with the sinusoidal signal using these mathematical expressions for the positive and negative lines for each term "n".This comparison with the sine signal and the respective positive and negative lines is shown in Figure 4.It is important to remember the importance of obtaining the midpoint of the triangular pulse, which is expressed as Once these points are obtained, we define the amplitude A t , which remains constant, and the slope, which does not vary.Substituting these new equations into the positive slope in Equation (11), we obtain the expression for the nth positive line y nt : Similarly, for the equation of the negative line y nt ', The triangular signal is compared with the sinusoidal signal using these mathematical expressions for the positive and negative lines for each term "n".This comparison with the sine signal and the respective positive and negative lines is shown in Figure 4.
In Figure 4, the sine signal is highlighted in red.It appears to be a straight line due to the considerable difference in frequency concerning the triangular signal.Although in more realistic models, the sine signal is not represented as a straight line, for practical purposes, it is simplified to facilitate the identification of the intersection point with the positive line.It is important to note that this intersection point coincides with the equation previously defined for the positive line.
By identifying the point of intersection between the triangular pulse signal and the sinusoidal signal, the start of the pulse is identified and expressed in angles for clarity and simplicity.This time expressed in angles is denoted as β 1 , corresponding to the beginning of the first pulse α 1 of the inverter signal.
This first angle, where the signal intersects the negative line, is defined in time as t 1 .The mathematical expressions of the sine signal are equated with the equation of the corresponding line, in this case, the equation of the negative line, obtaining the following equation: In Figure 4, the sine signal is highlighted in red.It appears to be a straight line due to the considerable difference in frequency concerning the triangular signal.Although in more realistic models, the sine signal is not represented as a straight line, for practical purposes, it is simplified to facilitate the identification of the intersection point with the positive line.It is important to note that this intersection point coincides with the equation previously defined for the positive line.
By identifying the point of intersection between the triangular pulse signal and the sinusoidal signal, the start of the pulse is identified and expressed in angles for clarity and simplicity.This time expressed in angles is denoted as β 1 , corresponding to the beginning of the first pulse α1 of the inverter signal.
This first angle, where the signal intersects the negative line, is defined in time as t 1 .The mathematical expressions of the sine signal are equated with the equation of the corresponding line, in this case, the equation of the negative line, obtaining the following equation: Here, for the sinusoidal signal, As represents the amplitude, ω is the carrier frequency (commonly 60 Hz), and t1 is when the first pulse becomes active, i.e., where the sine and straight line intersect (see Figure 4).However, solving t1 directly is complicated, because this equation is a transcendental function in which the variable cannot be explicitly cleared.Therefore, in this work, the use of numerical or graphical methods to obtain t1 is proposed.
To solve (16) numerically, this equation is equated to zero, and the following expression is obtained: This can be simplified to Here, for the sinusoidal signal, A s represents the amplitude, ω is the carrier frequency (commonly 60 Hz), and t 1 is when the first pulse becomes active, i.e., where the sine and straight line intersect (see Figure 4).However, solving t 1 directly is complicated, because this equation is a transcendental function in which the variable cannot be explicitly cleared.Therefore, in this work, the use of numerical or graphical methods to obtain t 1 is proposed.
To solve (16) numerically, this equation is equated to zero, and the following expression is obtained: 2A t t p (n t t p + t p − t 1 ) − A s sin(ωt 1 ) ≈ 0 (17) This can be simplified to The point at which this equation becomes zero is the time t 1 , which corresponds to angle β 1 for the first pulse (upstream pulse).A similar procedure is performed to obtain the downstream pulse, but the sine signal is compared to the positive straight line, as shown in Figure 5.
Therefore, the following equation is obtained: where t 1f is the final time of the pulse, and β 1f is the final angle corresponding to the time t 1f .Like the previous equation, it is equated to zero, obtaining the following equations: The point at which this equation becomes zero is the time t1, which corresponds to angle β1 for the first pulse (upstream pulse).A similar procedure is performed to obtain the downstream pulse, but the sine signal is compared to the positive straight line, as shown in Figure 5. Therefore, the following equation is obtained: where t1f is the final time of the pulse, and β 1f is the final angle corresponding to the time t1f.Like the previous equation, it is equated to zero, obtaining the following equations:

Numerical Evaluation of the Angles
As can be seen in Figures 4 and 5, the values of t1 and t1f are within the range . Therefore, it is possible to define a sampling range named dm.
The solution of Equations ( 18) and ( 21) will always be in this sampling range.To find the solution, the sampling range is divided into Ns samples, and Equations ( 18) and ( 21) are evaluated for each sample.The value closest to zero will be considered the equation's

Numerical Evaluation of the Angles
As can be seen in Figures 4 and 5, the values of t 1 and t 1f are within the range n t t p < t 1 < t p n t + 1 2 .Therefore, it is possible to define a sampling range named dm.
The solution of Equations ( 18) and ( 21) will always be in this sampling range.To find the solution, the sampling range is divided into Ns samples, and Equations ( 18) and ( 21) are evaluated for each sample.The value closest to zero will be considered the equation's solution.The greater the samples' Ns, the more accurate the solution to the equation will be.The precision is sufficient for this application with Ns = 10, but it is possible to use greater values.
The sampling vector for the positive slope is The sampling vector for the negative slope is Once the times t 1 and t 1f are obtained for the first pulse, the angles can be calculated with the following expressions: where ω is the angular frequency and α 1 is the width of the first pulse, which, as seen in the equation, is the subtraction of β 1f minus β 1 .However, this process must be repeated for each triangular pulse signal.The complete algorithm for angle calculation is detailed in the flowchart of Figure 6.
where  is the angular frequency and α 1 is the width of the first pulse, which, as seen in the equation, is the subtraction of β 1f minus β 1 .However, this process must be repeated for each triangular pulse signal.
The complete algorithm for angle calculation is detailed in the flowchart of Figure 6.In this flowchart, mu represents one of the vector's M and M′ components.The process is as follows: first, the specifications are defined, and the main variables are calculated after the first angle of the negative slope line is evaluated (β1).For this, the times (tn−) for each component of vector M′ are obtained, and Equation ( 18) is an assessment for each one, generating the error vector (en−).Then, the minimum value of vector en− is obtained (emin) after the value of time (t1−) corresponding to this emin is obtained with the corresponding angle value β1.Subsequently, the same process is undertaken to assess the final angle of the first pulse (β1f), resolving Equation ( 21  In this flowchart, mu represents one of the vector's M and M ′ components.The process is as follows: first, the specifications are defined, and the main variables are calculated after the first angle of the negative slope line is evaluated (β 1 ).For this, the times (t n− ) for each component of vector M ′ are obtained, and Equation ( 18) is an assessment for each one, generating the error vector (e n− ).Then, the minimum value of vector e n− is obtained (e min ) after the value of time (t 1− ) corresponding to this e min is obtained with the corresponding angle value β 1 .Subsequently, the same process is undertaken to assess the final angle of the first pulse (β 1f ), resolving Equation ( 21) using the solutions vector M. The process continues for each pulse until the pulse Na.The algorithm assesses the pulse for only one quarter of the wave, since the rest of the angles are evaluated for symmetry.

Results
The main objective of our work is to establish an initial proof of concept of the proposed algorithm.Using single-phase inverters, we simplified the model and concentrated on validating the algorithm's fundamental principles without introducing the additional complexity involved in three-phase systems.This initial approach is essential to identifying and solving basic problems before scaling to more complex systems.
Furthermore, the principles and methodology of the proposed algorithm are directly applicable to three-phase systems, although they are not explicitly realized in this work.The transition from single-phase to three-phase systems typically involves phase shifting the modulation signals, which is a straightforward extension once the single-phase system proves effective.

Case I. Application with a Frequency Modulation m f = 12
Two examples were developed to assess the algorithm's efficacy: A simple example was performed to observe the behavior and test the previously established equations.Table 1 was generated to determine the important initial parameters that do not require additional calculations.Having these initial parameters, we use the equations previously seen to calculate the other relevant parameters shown in Table 2. Table 2 shows that only three angles must be calculated for this example.The period of the triangular signal t p , which represents the duration of the signal, is the inverse of its frequency f c , resulting in 0.083 s.Thus, the following angles were obtained and are shown in Table 3.
Table 3. Angles obtained by the algorithm.

Angles
Value Here, β 1 = β n (1)− and β 1f = β n (1)+ , respectively.As can be seen in Table 3, these are angles for a quarter-cycle.To calculate the remaining angles at the half-cycle, subtract 180 minus the angle obtained.For 3  4 of a cycle, 180 is added to the obtained angles.For the final angles, subtract 360 minus the obtained angles, resulting in Table A1 of Appendix A. These angles calculated in Tables 3 and A1  Table 2 shows that only three angles must be calculated for this example.The period of the triangular signal t p , which represents the duration of the signal, is the inverse of its frequency f c , resulting in 0.083 s.Thus, the following angles were obtained and are shown in Table 3.

Angles
Value Here, β 1 = β n (1)-and β 1f =β n (1)+ , respectively.As can be seen in Table 3, these are angles for a quarter-cycle.To calculate the remaining angles at the half-cycle, subtract 180 minus the angle obtained.For 3 4 of a cycle, 180 is added to the obtained angles.For the final angles, subtract 360 minus the obtained angles, resulting in Table A1 of Appendix A. These angles calculated in Tables 3 and A1 can be used to construct the PWM signal.Each angle obtained represents the time the signal state should be changed, either up or down, as shown in Figure 7.The obtained signal is compared in this figure with the sinusoidal fundamental component in the Fourier series.

Fourier Series Decomposition of Case I
An important aspect when obtaining this signal is to evaluate how close our generated square signal is to the sinusoidal signal.For this, it is necessary to calculate the Fourier series corresponding to the waveform obtained.Since our signal is quarter-wave sym-

Fourier Series Decomposition of Case I
An important aspect when obtaining this signal is to evaluate how close our generated square signal is to the sinusoidal signal.For this, it is necessary to calculate the Fourier series corresponding to the waveform obtained.Since our signal is quarter-wave symmetry, the coefficients a 0 and a n of the Fourier series are equal to zero, which leaves only the coefficient b n .The equation to calculate this coefficient would be where V dc is the dc voltage feeding the inverter, n represents the odd harmonics, β k represents the angles corresponding to the positive pulses, β kf represents the angles corresponding to the negative pulses, and β Na is the last angle calculated for a quarter-wave.In this case, being three pulses, the equation would be simplified as follows: With Equation (30), we can calculate the total harmonic distortion (THD) up to the 49th harmonic to evaluate the quality of our signal concerning the sine wave, obtaining the following THD equation: Thus, the following graph is obtained, as shown in Figure 8. where V dc is the dc voltage feeding the inverter, n represents the odd harmonics, β k represents the angles corresponding to the positive pulses, β kf represents the angles corresponding to the negative pulses, and β Na is the last angle calculated for a quarter-wave.
In this case, being three pulses, the equation would be simplified as follows: With Equation (30), we can calculate the total harmonic distortion (THD) up to the 49th harmonic to evaluate the quality of our signal concerning the sine wave, obtaining the following THD equation: Thus, the following graph is obtained, as shown in Figure 8. Figure 8 shows the harmonics up to harmonic 49, where the first harmonic is in blue, and the odd harmonics are in green.These harmonics are expressed in percentage and normalized concerning the first harmonic, which has a value of 100%.It is observed that the most significant and largest harmonics will appear on the opposite sides of the carrier frequency, which in this case is 12.The largest harmonics are in 11th and 13th place, gradually decreasing to the ninth and fifteenth.The 11th and 13th harmonics are about 20% of the fundamentals, while the ninth and fifteenth are about 17% and 18%.From these harmonics onwards, the other harmonics decrease in amplitude.The largest harmonics, always appearing on the sides of the carrier frequency, are the ones of interest to eliminate.This can be achieved by placing a filter at the output, which will significantly reduce their amplitude and improve the efficiency and cost of the system.An important parameter to calculate is the THD, which is the sum of the squares of all harmonics up to 49, according to IEEE 519.In this case, the THD obtained is 52.63%.Figure 8 shows the harmonics up to harmonic 49, where the first harmonic is in blue, and the odd harmonics are in green.These harmonics are expressed in percentage and normalized concerning the first harmonic, which has a value of 100%.It is observed that the most significant and largest harmonics will appear on the opposite sides of the carrier frequency, which in this case is 12.The largest harmonics are in 11th and 13th place, gradually decreasing to the ninth and fifteenth.The 11th and 13th harmonics are about 20% of the fundamentals, while the ninth and fifteenth are about 17% and 18%.From these harmonics onwards, the other harmonics decrease in amplitude.The largest harmonics, always appearing on the sides of the carrier frequency, are the ones of interest to eliminate.This can be achieved by placing a filter at the output, which will significantly reduce their amplitude and improve the efficiency and cost of the system.An important parameter to calculate is the THD, which is the sum of the squares of all harmonics up to 49, according to IEEE 519.In this case, the THD obtained is 52.63%.
The obtained results are congruent with the characteristic behavior of the natural or digital SPWM [8].Therefore, the behavior validates the assessment of the proposed algorithm.Another example was developed with a higher carrier frequency f c to apply the algorithm to a more realistic scenario.In the SPWM technique, the harmonics will be displaced to the harmonic corresponding to the frequency modulation m f ; with m f = 48, the harmonics will be displaced to harmonic 49.A small passive LC filter can eliminate the harmonics in this range.Therefore, the THD will reduce considerably complying with IEEE 519 standard.Therefore, a new scenario has been proposed that simulates a more rigorous situation aligned with current standards, where the proposed technique's response to a higher number of harmonics is thoroughly evaluated.
This new case has been specifically designed to comply with the requirements established by the IEEE 519 standard, which establishes rigorous limits for harmonics present in electrical systems.According to the standard, the main objective of this additional analysis is to verify the technique's ability to maintain the THD within acceptable limits even when considering harmonics up to number 50.
To achieve this goal, a detailed analysis of the switching angles and the resulting signal has been carried out, ensuring that the requirements established by the regulations are met.It is important to highlight that the main focus of this new case is to ensure that the THD remains within the acceptable limits set by the regulations, even considering a higher number of harmonics.
The specifications for this case are the follows: Table 4 shows the main specifications illustrated in Fig. 1 and the proposed number of samples Ns.Table 5 shows the rest of the parameters to be calculated from the specifications of Table 4.The results and detailed analysis for this new case are presented below.The technique's effectiveness will be evaluated in a more demanding context and closer to real operating conditions.The algorithm was modified to calculate the angles corresponding to the new case, ensuring that the output would not present harmonics before harmonic 50, as previously mentioned.As a result of this adaptation, the specific angles for this case were obtained, which are detailed in Table A2 in Appendix A.
The data presented in Table A2 illustrate an increase in the required angles to generate harmonics on the sides of the 50th harmonic (23 angles were obtained).The corresponding signal generated by these angles is depicted in Figure 9, providing evidence of the algorithm's efficiency in determining the angles necessary for this new case.The results and detailed analysis for this new case are presented below.The technique's effectiveness will be evaluated in a more demanding context and closer to real operating conditions.The algorithm was modified to calculate the angles corresponding to the new case, ensuring that the output would not present harmonics before harmonic 50, as previously mentioned.As a result of this adaptation, the specific angles for this case were obtained, which are detailed in Table A2 in Appendix A.
The data presented in Table A2 illustrate an increase in the required angles to generate harmonics on the sides of the 50th harmonic (23 angles were obtained).The corresponding signal generated by these angles is depicted in Figure 9, providing evidence of the algorithm's efficiency in determining the angles necessary for this new case.As mentioned, assessing the signal's harmonic content is relevant because it introduces more angles and pulses, ultimately affecting the total harmonic distortion (THD) and harmonic values.The THD analysis acquired is presented in Figure 10, indicating that the most significant harmonics now appear on the opposite sides of the carrier frequency 50, specifically at harmonics 49 and 47, with 22% and 16%, respectively.Notably, the harmonics before these exhibit a practically null percentage, around 0%.As mentioned, assessing the signal's harmonic content is relevant because it introduces more angles and pulses, ultimately affecting the total harmonic distortion (THD) and harmonic values.The THD analysis acquired is presented in Figure 10, indicating that the most significant harmonics now appear on the opposite sides of the carrier frequency 50, specifically at harmonics 49 and 47, with 22% and 16%, respectively.Notably, the harmonics before these exhibit a practically null percentage, around 0%.One way to reduce harmonic distortion is to incorporate a filter at the output, which reduces the amplitude of these harmonics [21], ultimately enhancing system efficiency.The THD obtained for this case was 34.20%, significantly improving over the previous scenario with fewer angles.This indicates a successful algorithm adaptation to comply with IEEE 519 and provide favorable signal quality and system efficiency results.One way to reduce harmonic distortion is to incorporate a filter at the output, which reduces the amplitude of these harmonics [21], ultimately enhancing system efficiency.The THD obtained for this case was 34.20%, significantly improving over the previous scenario with fewer angles.This indicates a successful algorithm adaptation to comply with IEEE 519 and provide favorable signal quality and system efficiency results.

Discussion
Traditionally, the digital sampling of the SPWM technique has consisted of the digital generation of the triangular and sinusoidal signals; the obtained lists occupy a considerable amount of memory and require relatively sophisticated devices such as FPGAs [8][9][10]13,17].The proposed algorithm allows direct calculation of the switching angles without the need for digital comparison of the triangular and sinusoidal signals.This allows for reducing the use of memory, and the simplicity of the algorithm allows its implementation in low-cost processors such as generic microcontrollers or microcomputers like Raspberry Pi.
The evaluation of THD in the presented cases demonstrated validity of the proposed algorithm to evaluate theoretically and numerically the switching angles originated by the SPWM technique.
The proposed algorithm is simple and can be easily reconfigurable by changing the initial conditions such as the frequency modulation index m f , which allows the elimination of harmonics up to a value determined by the switching speed of the processor used.For example, when m f = 100, the digitally SPWM will eliminate the harmonics until harmonic 100.
The accuracy in assessing the angles depends on the number of samples N s ; the greater the number of samples, the greater the accuracy.For the analyzed cases, 10 samples were sufficient.
Digitally, SPWM presents more robustness than the natural SPWM; its implementation is more economical and smaller.The reconfiguration capability gives it more flexibility.

Conclusions
This paper presented an algorithm to assess the switching angles in SPWM inverters with a simple algorithm that includes theoretical and numerical calculus.The algorithm starts from the theoretical evaluation of the zero crossings of the triangular signal and the reference sinusoidal signal.The resulting transcendental equation is solved numerically by obtaining the minimum value of a series of previously defined samples.The algorithm is validated by obtaining the THD of the generated signal.Two cases are considered, one with a frequency modulation of 12 to test the algorithm and the other more practical, with a frequency modulation of 48.The results show that the harmonics obtained coincide with those obtained with the natural SPWM, validating the theoretical calculations.
The initial specifications can be easily changed to shift the generated harmonics to frequencies as high as supported by the device in which the algorithm is implemented.This allows for reducing the same proportion that the L or LCL filter required to filter the high-frequency harmonics, decreasing the cost and volume of the inverter.
The proposed algorithm is limited to single-phase systems and unipolar SPWM, but it can easily be applied to three-phase systems.Table A2 shows the 23 angles obtained for a quarter-wave so that the signal has no harmonics up to the 50th harmonic.

Figure 1 .
Figure 1.Analog implementation of the unipolar SPWM with an op-amp.First signal: sinusoidal signal, second: triangular signal, third: output of the Op-amp, fourth: SPWM signal in the output of the inverter.

Figure 1 .
Figure 1.Analog implementation of the unipolar SPWM with an op-amp.First signal: sinusoidal signal, second: triangular signal, third: output of the Op-amp, fourth: SPWM signal in the output of the inverter.

Figure 2 .
Figure 2. Triangular signal lines; the positive line is named "y" and negative line is named "y'".

Figure 2 .
Figure 2. Triangular signal lines; the positive line is named "y" and negative line is named "y'".

Figure 3 .
Figure 3.The triangle for nt pulse.

Figure 3 .
Figure 3.The triangle for n t pulse.

Figure 4 .
Figure 4. Obtention of the first angle (β1) of the comparison between the triangle signal and sine signal.

Figure 4 .
Figure 4. Obtention of the first angle (β 1 ) of the comparison between the triangle signal and sine signal.
can be used to construct the PWM signal.Each angle obtained represents the time the signal state should be changed, either up or down, as shown in Figure7.The obtained signal is compared in this figure with the sinusoidal fundamental component in the Fourier series.Algorithms 2024, 17, x FOR PEER REVIEW 11 of 18

Figure 7 .
Figure 7. Application of the calculated switching angles (blue) compared with the sinusoidal fundamental component (red).

Figure 7 .
Figure 7. Application of the calculated switching angles (blue) compared with the sinusoidal fundamental component (red).

Figure 8 .
Figure 8. Odd harmonics and their percentage concerning the fundamental component.

Figure 8 .
Figure 8. Odd harmonics and their percentage concerning the fundamental component.

3. 3 .
Case II.Application with a Frequency Modulation m f = 48

Algorithms 2024 , 18 Figure 10 .
Figure 10.Odd harmonics and their percentage concerning the fundamental harmonic.

Figure 10 .
Figure 10.Odd harmonics and their percentage concerning the fundamental harmonic.
) using the solutions vector M. The process

Table 3 .
Angles obtained by the algorithm.