## 3 Learning Controller Design Using Fourier Series  
In this section, the Fourier series learning controller is presented for linear and nonlinear systems. An update law for their coefficients, which assures the system stabilization without the help of other controllers, is shown. The harmonics of the error dynamics are used by the controller, and they are obtained from the discrete Fourier transform (DFT). Thus, the error dynamics does not need to be a periodic function. A function, that represent the dynamics error of the system, is considered to design the learning controller:  
\[s(t)=e(t)+\dot{e}(t) \tag{14}\]  
where \(e(t)\) is the error of the system. For this case, \(e(t)=\omega_{d}-\omega\) is the difference between the desired velocity and the actual velocity of the PMSM. The controller is designed to decrease the function represented by Equation (14). Each sampling period \(T_{s}\) is taking a sample of \(s(t)\), and data windows with \(N\) terms are formed as \([w_{0}^{k},w_{1}^{k},...,w_{N-1}^{k}]\). The sample \(s^{k}\) in the \(k\)-th instant is assigned to \(w_{N-1}^{k}\), and other values are shifted to the left. Namely, \(w_{N-1}^{k}=s^{k},\quad w_{N-2}^{k}=s^{k-1},\quad w_{N-3}^{k}=s^{k-2},\quad... \quad,\quad w_{0}^{k}=s^{k-(N-1)}\). Each data window \(w_{j}^{k}\) has a sequence of discrete numbers that can be transformed to the frequency domain by the DFT as follows:  
\[X_{n}^{k}=\sum_{j=0}^{N-1}w_{j}^{k}e^{-i\frac{2\pi nj}{N}},\quad\quad\quad n=0,1,...,N-1 \tag{15}\]  
Note that a data window \(w_{j}^{k}\) with \(N\) real elements is enough to calculate the DFT from Equation (15). Although the elements of the data window are random numbers, the DFT can be calculated. Then, the signal \(s(t)\) does not need to be periodic. Furthermore, using the Euler formula in Equation (15), the real and imaginary parts of \(X_{n}^{k}\) are:  
\[z_{n}^{k}=Re\{X_{n}^{k}\}=\sum_{j=0}^{N-1}w_{j}^{k}\text{cos}\left(\frac{2\pi n j }{N}\right),\quad\quad\quad n=0,1,...,N-1 \tag{16}\]  
\[y_{n}^{k}=Im\{X_{n}^{k}\}=-\sum_{j=0}^{N-1}w_{j}^{k}\text{sin}\left(\frac{2 \pi nj}{N}\right),\quad\quad\quad n=0,1,...,N-1 \tag{17}\]  
Then, the coefficients to return from the frequency domain to the time domain in the \(k\)-th instant are calculated as follow:  
\[p_{0}^{k}=\frac{z_{0}^{k}}{N},\quad\quad\quad q_{0}^{k}=0 \tag{18}\]  
\[p_{\frac{N}{N}}^{k}=\frac{z_{\frac{N}{N}}^{k}}{N},\quad\quad\quad q_{\frac{N} {N}}^{k}=0 \tag{19}\]\[p_{n}^{k}=\frac{2z_{n}^{k}}{N},\hskip 28.452756 ptn=1,...,\frac{N}{2}-1 \tag{20}\]  
\[q_{n}^{k}=-\frac{2y_{n}^{k}}{N},\hskip 28.452756 ptn=1,...,\frac{N}{2}-1 \tag{21}\]  
From the Inverse Discrete Fourier Transform (IDFT), taking the values of Equation (15) and considering \(N\) as an even number, the representation in Fourier series for samples in the \(k\)-th window \(w_{j}^{k}\) is:  
\[w_{j}^{k}=\sum_{n=0}^{N/2}\left(p_{n}^{k}\text{cos}\left(\frac{2\pi\text{ }nj}{N}\right)+q_{n}^{k}\text{sin}\left(\frac{2\pi\text{}nj}{N}\right)\right),\hskip 28.452756 ptj=0,1,...,N-1 \tag{22}\]  
Equation (22) means that minimizing the coefficients \(p_{n}^{k}\) and \(q_{n}^{k}\), it is possible to decrease \(w_{j}^{k}\). The controller proposed is a Fourier series with adjustable coefficients, where each coefficient is modified to contribute to the \(p_{n}^{k}\) and \(q_{n}^{k}\) decrease in each sample period. Equation (22) is the Fourier series representation of the dynamics error. The new controller uses the coefficients of this representation, but each coefficient is modified by a gain and adding a special function, which can be seen intuitively as an integral part.