# Demystifying DFT-Based Harmonic Phase Estimation, Transformation, and Synthesis
## 2 Robust DFT-Based Phase Estimation of Individual Sinusoids
### DFT and the Rectangular Window  
In this subsection, the frequency domain representation of our signal (5) is obtained by computing the DFT according to (4) when the window is the rectangular window:  
\[w[n]=w_{R}[n]=1,\ n=0,1,\dots,N-1\,. \tag{7}\]  
Figure 3 represents the magnitude of the frequency response of the rectangular window, i.e., \(\left|W_{R}\big{(}e^{i\omega}\big{)}\right|=\left|\sum_{n=0}^{N-1}w_{R}[n]e^{ -i\omega n}\right|\), in the range \(-8\pi/N\leq\omega\leq 8\pi/N\), and after the gain is normalized such that the maximum gain is the unity. Two other frequency responses are also represented in Figure 3, which will be discussed in subsequent subsections. The frequency axis in Figure 3 is normalized by \(2\pi/N\) such that integer numbers in this normalized axis can be read as bin indices in a DFT filter bank interpretation. Given that both \(x[n]\) and \(w[n]\) in (4) are real-valued, then \(X[k]\) is known to be conjugate-symmetric, i.e., \(X[k]=X^{*}[N-k]\), \(k=1,2,\dots,N/2-1\), where \((\cdot)^{*}\) denotes complex conjugation. This means that unique information provided by \(X[k]\) lies in the range \(k=0,1,\dots,N/2\). Using Euler trigonometric relations and ignoring the Dirichlet kernel (i.e., a function with the form \(\sin(\theta)/\sin(\theta/N)\)) that is centered outside this range of \(k\) and that causes spectral leakage, we are left with the following:  
\[X[k]\approx\frac{A}{2}e^{i\left(\phi_{\ell}-\frac{\pi}{2}+\pi(k_{\ell}+\Delta _{\ell}-k)\left(1-\frac{1}{N}\right)\right)}\frac{\sin\pi(k_{\ell}+\Delta_{ \ell}-k)}{\sin\frac{\pi}{N}(k_{\ell}+\Delta_{\ell}-k)}. \tag{8}\]  
Figure 3: Normalized magnitude of the frequency response of the rectangular, sine, and Hanning windows in the range \(-8\pi/N\leq\omega\leq 8\pi/N\). The frequency axis (\(\omega\)) is normalized by \(2\pi/N\). Given that the width of the main lobe of the frequency response of the rectangular window is \(4\pi/N\), as apparent in Figure 3, then, depending on the value of \(\Delta_{\ell}\), at most two spectral lines with a non-zero magnitude fall within that main lobe width. If \(\Delta_{\ell}\in[-0.5,0.5]\), then the spectral line \(k=k_{\ell}\) corresponds to a local maximum in the magnitude spectrum, as illustrated in Figure 4:  
This means that this spectral line is more likely to be less affected by noise and leakage, which makes it particularly suitable for phase estimation. In fact, if \(k=k_{\ell}\), then from (8), we obtain \(\angle X[k_{\ell}]=\phi_{\ell}-\frac{\pi}{2}+\pi\Delta_{\ell}\Big{(}1-\frac{1 }{N}\Big{)}\), which leads to the following:  
\[\phi_{\ell}=\angle X[k_{\ell}]+\frac{\pi}{2}-\pi\Delta_{\ell}\bigg{(}1-\frac{ 1}{N}\bigg{)}. \tag{9}\]  
Despite being usable, this result presents one practical difficulty since it requires that the fractional frequency (\(\Delta_{\ell}\)) be estimated as accurately as possible [5, 18]. This means that frequency estimation errors may propagate to the phase estimation. In order to avoid this, a more robust approach is available if, instead of estimating phase with respect to the origin of the time segment, estimation is performed with respect to the group delay of the DFT filter bank. Given that the window is symmetric, the group delay is constant and given by \(\tau=(N-1)/2\). Therefore, using this result and (6) and (9), we obtain the following:  
\[\hat{\phi}_{\ell} = \angle X[k_{\ell}]+\frac{\pi}{2}-\pi\Delta_{\ell}\bigg{(}1-\frac {1}{N}\bigg{)}+\omega_{\ell}\tau \tag{10}\] \[= \angle X[k_{\ell}]+\frac{\pi}{2}+\pi k_{\ell}\bigg{(}1-\frac{1}{ N}\bigg{)}\,\]  
which represents a more robust phase estimator that just depends on the bin index of the spectral line corresponding to the local maximum in the magnitude spectrum, and on the phase of that spectral line. A perspective on the estimation error that is associated with (10) can be given by a simple metric that evaluates the cumulative distance, or error, between the estimated \(\hat{\phi}\), and the corresponding ground-truth value, \(\phi\), when the latter varies in the range \([-\pi,\pi]\).