DSPRelated.com
Blogs

Phase and Amplitude Calculation for a Pure Complex Tone in a DFT

Cedron DawgJanuary 6, 2018

Introduction

This is an article to hopefully give a better understanding of the Discrete Fourier Transform (DFT) by deriving exact formulas to calculate the phase and amplitude of a pure complex tone from a DFT bin value and knowing the frequency. This is a much simpler problem to solve than the corresponding case for a pure real tone which I covered in an earlier blog article[1]. In the noiseless single tone case, these equations will be exact. In the presence of noise or other tones they will be estimates.

Unlike most of my previous articles, I don't believe the equations derived in this article are original. However, trying to find them online has been an elusive undertaking. Readers are welcome to add references in the comments. Any approach based on interpolation of the Sinc function will be an approximation and is not the same. On the other hand, references including the Dirichlet Kernel may be correct as this derivation is essentially similar in nature.

A Pure Complex Tone

The same variables and equation for describing the tone from my previous articles will be used. The variables are amplitude ($ M $), frequency ($ f $), phase ($ \phi $), and the sample count in the frame ($ N $). The signal definition is: $$ S_n = M \cdot { e^{ i(\alpha n + \phi) } } \tag {1} $$ Where: $$ \alpha = f \cdot \frac{ 2\pi }{ N } \tag {2} $$ The same $1/N$ normalized DFT definition is also used. $N$ is also the number of bins in the DFT so $k$ ranges from $0$ to $N-1$. $$ Z_k = \frac{ 1 }{ N } \sum_{n=0}^{N-1} { S_n e^{ -i\beta_k n } } \tag {3} $$ Where: $$ \beta_k = k \cdot \frac{ 2\pi }{ N } \tag {4} $$

Bin Value Formula for a Pure Complex Tone

This is a formula to calculate the bin values of a DFT for a pure complex tone: $$ Z_k = \frac{ M }{ N } e^{ i \phi } \cdot \frac{ 1 - e^{ i\alpha N } }{ 1 - e^{ i(\alpha - \beta_k ) } } \tag {5} $$ The derivation of this formula from (1) and (3) can be found in my previous blog article titled "DFT Bin Value Formulas for Pure Complex Tones."[2] The formula has a pluggable discontinuity in the integer frequency case at the bin corresponding to the frequency ($f=k$). $$ Z_k = M e^{ i \phi } \tag {6} $$ Proving that (6) is the limit of (5) as $\alpha$ approaches $\beta_k$ only takes a simple application of L'Hôpital's rule.

Finding the Frequency

If the frequency is known ahead of time, only one DFT bin value needs to be calculated in order to calculate the phase and the amplitude of the tone. For best results it should be the DFT bin closest to the frequency of the sampled tone.

If the frequency of the tone is not known, the two or three DFT bin values nearest the peak magnitude bin can be used to find it. If the magnitude of a bin adjacent to the peak is fairly large, the two bin frequency calculation formula should be used[3]. If the bins adjacent to the peak are small in comparison, the three bin formula[4] centered at the peak should be used. In either case, the frequency can be calculated. In the noiseless single tone case, the calculation will be exact.

Single Bin Calculation

The unknown values to be solved for in the signal definition are the phase ($\phi$) and the amplitude ($M$). Both of these can be isolated from (5). $$ M e^{ i \phi } = N \cdot \frac{ 1 - e^{ i(\alpha - \beta_k ) } }{ 1 - e^{ i\alpha N } } \cdot Z_k = Q \tag {7} $$ The value of $Q$ can be directly calculated from the center expression. In the case of an integer frequency at the frequency bin, the value of $Q$ is simply the same as the bin value ($Z_k$).

Thanks to the polar form of complex numbers, once the value of Q is known finding the values of $\phi$ and $M$ is very straightforward. $$ \phi = \arg( Q ) \tag {8} $$ $$ M = \| Q \| \tag {9} $$

Simplifying the Calculation of Q

The calculation of $Q$ from (7) looks simple enough mathematically, but computationally it is quite involved. With a little bit of more work, formulas for $\phi$ and $M$ can be found that are significantly easier to compute.

The derivation starts by changing the frame of reference of the frequency term to be bin centered. $$ \delta = \alpha - \beta_k \tag {10} $$ $$ \alpha = \delta + \beta_k \tag {11} $$ The next step is to basically undo one of the simplifying assumption made in the derivation of (5). $$ e^{i\alpha N} = e^{i (\delta + \beta_k) N} = e^{i \delta N } e^{i \beta_k N} = e^{i \delta N } e^{i 2 \pi k} = e^{i \delta N } 1^k = e^{i \delta N } \tag {12} $$ The definition of the Sine function in exponential terms will also be needed. $$ \sin( x ) = \frac{ e^{ i x } - e^{ -i x } }{ 2 i } \tag {13} $$ The fractional part in (7) can now be computationally simplified. $$\begin{aligned} \frac{ 1 - e^{ i(\alpha - \beta_k ) } }{ 1 - e^{ i\alpha N } } &= \frac{ 1 - e^{ i\delta } }{ 1 - e^{ i\delta N } } \\ &= \frac{ e^{ i\frac{\delta}{2} } \left( e^{ -i\frac{\delta}{2} } - e^{ i\frac{\delta}{2} } \right) } { e^{ i\frac{\delta N}{2} } \left( e^{ -i\frac{\delta N }{2} } - e^{ i\frac{\delta N}{2} } \right) } \\ &= e^{ i\frac{\delta}{2}(1-N) } \cdot \frac{ e^{ i\frac{\delta}{2} } - e^{ -i\frac{\delta}{2} } } { e^{ i\frac{\delta N }{2} } - e^{ -i\frac{\delta N}{2} } } \\ &= e^{ i\frac{\delta}{2}(1-N) } \cdot \frac{ \frac{ e^{ i\frac{\delta}{2} } - e^{ -i\frac{\delta}{2} } }{2i} } { \frac{ e^{ i\frac{\delta N }{2} } - e^{ -i\frac{\delta N}{2} } }{2i} } \\ &= e^{ i\frac{\delta}{2}(1-N) } \cdot \frac{ \sin( \frac{\delta}{2} ) }{ \sin( \frac{\delta N }{2} ) } \\ \end{aligned} \tag {14} $$ The bin value can be expressed in polar form. $$ Z_k = r e^{ i \theta } \tag {15} $$ Where: $$ r = \| Z_k \| \tag {16} $$ $$ \theta = \arg( Z_k ) \tag {17} $$ The results of (14) and (15) can be substituted into (7) to get this: $$ Q = N \cdot e^{ i\frac{\delta}{2}(1-N) } \cdot \frac{ \sin( \frac{\delta}{2} ) }{ \sin( \frac{\delta N }{2} ) } \cdot r e^{ i \theta } \tag {18} $$ Finally, the exponential expressions can be combined, and the equation rearranged a little to make it neater. $$ Q = r \cdot \frac{ N \sin( \frac{\delta}{2} ) }{ \sin( \frac{\delta N }{2} ) } \cdot e^{ i \left[ \theta - \frac{\delta}{2}(N-1) \right] } \tag {19} $$

The Final Results

Since (19) is in complex polar form, obtaining the angle and magnitude is very straightforward and can be read right from the equation. $$ \phi = \arg( Q ) = \theta - \frac{\delta}{2}(N-1) \mod 2 \pi \tag {20} $$ $$ M = \| Q \| = r \cdot \left| \frac{ N \sin( \frac{\delta}{2} ) }{ \sin( \frac{\delta N }{2} ) } \right| \tag {21} $$ The formulas are a clearer conceptually with the bin value restored. $$ \phi = \arg( Z_k ) - \frac{\delta}{2}(N-1) \mod 2 \pi \tag {22} $$ $$ M = \| Z_k \| \cdot \left| \frac{ N \sin( \frac{\delta}{2} ) }{ \sin( \frac{\delta N }{2} ) } \right| \tag {23} $$ Notice that absolute value bars have been placed around the Sine fraction to keep the amplitude positive. It is clear from (19) that if the value inside the absolute value bars is negative then $\pi$ should be added to the phase value. As long as the calculation is done on the peak bin, this won't happen.

Sample Calculation

The actual calculations of the phase and amplitude values, in the simplified form, are very straighforward. This is actual code that does the calculations:

...

//--- Calculate Angle Values

        double alpha  = freq * 2.0 * M_PI / (double) N;
        double beta_k = k    * 2.0 * M_PI / (double) N;
        double delta      = alpha - beta_k;
        double half_delta = delta * .5;

//--- Calculate Phase

        double theta = atan2( bins[k].Imag, bins[k].Real );
        double phi = theta - half_delta * (double) ( N - 1 );

//--- Calculate Amplitude

        double r = sqrt( bins[k].Real * bins[k].Real
                       + bins[k].Imag * bins[k].Imag );
        double num = (double) N * sin( half_delta );
        double den = sin( (double) N * half_delta );
        double M = r * num / den;

//--- Print the Calculation Values

...
It is written for clarity and it is not optimized, but it is likely the compiler will do that.

This is the value display of the calculations. Please note that the actual values used in the calculation are higher precision than the numbers which are displayed.

***********************************************************
Signal Definition:

  S_n = 6.7890 * e ^ i[ (5.4321*2PI/16)n + 1.2345 ]

Given Values:

  k   = 5   
  N   = 16  
  Z_k = -3.9414 + i * 2.9007
  f   = 5.4321

Angle Calculations:   

  alpha  = 5.4321 * 2 PI / 16 = 2.1332
  beta_k = 5.0000 * 2 PI / 16 = 1.9635

  delta = alpha - beta_k = 2.1332 - 1.9635 = 0.1697

  delta/2 = 0.1697 / 2 = 0.0848

Phase Calculation:

  theta = arg(Z_k) = atan2( 2.9007, -3.9414 ) = 2.5071 

  phi = theta - delta/2 * (N-1)
      = 2.5071 - 0.0848 * 15
      = 1.2345

Amplitude Calculation:

  r = ||Z_k|| = sqrt( -3.9414 ^ 2 + 2.9007 ^ 2 ) = 4.8937 

  num = N * sin( delta/2 )
      = 16 * sin( 0.0848 ) = 1.3559 

  den = sin( N * delta/2 )
      = sin( 16 * 0.0848 ) = 0.9773 

  M = r * num / den
    = 4.8937 * 1.3559 / 0.9773 = 6.7890 

***********************************************************
These calculations can be verified with a calculator, or by hand with some trig tables.

Conclusion

The phase and amplitude calculations of a pure complex tone from DFT bin values are significantly simpler than for a real valued tone. The derivation presented in this article is not very complicated and the results are easy to verify. It is important to keep in mind that these equations are exact as no approximation were used in their derivation. The study of the robustness of these formulas in the presence on noise is deferred until later.

References

[1] Dawg, Cedron, Phase and Amplitude Calculation for a Pure Real Tone in a DFT: Method 1

[2] Dawg, Cedron, DFT Bin Value Formulas for Pure Complex Tones

[3] Dawg, Cedron, A Two Bin Exact Frequency Formula for a Pure Complex Tone in a DFT

[4] Dawg, Cedron, Three Bin Exact Frequency Formulas for a Pure Complex Tone in a DFT



To post reply to a comment, click on the 'reply' button attached to each comment. To post a new comment (not a reply to a comment) check out the 'Write a Comment' tab at the top of the comments.

Please login (on the right) if you already have an account on this platform.

Otherwise, please use this form to register (free) an join one of the largest online community for Electrical/Embedded/DSP/FPGA/ML engineers: