2
$\begingroup$

Ladies, Gentlemen, Please let me again employee you with my problems. In following program there is only one sinusoid with frequency 256 Hz amplitude 100,000 and phase 0.67 pi rad. Testing frequencies grid is of 1 Hz step, from 0 Hz to 1000 Hz. Output has maximum amplitude 99,221 and phase 0.670561 pi rad at 256 Hz. I regard it okay given errors in accuracy.

But there are ups and downs in pseudo-frequencies amplitude while my understanding about DFT is that they should going upward till 256 Hz and then downward. Why these ups and downs? I remain with regards.

// Grid is created by variables lowTestFreq and testFreqStep
// both of double type, and integer type M.
// In comments are units of variables.

#include <iostream>
#include "math.h"

int main()
{  
    //Unchanged variables.
    double pi = 3.141592653589793; //pure number.
    int n = 0; // sample.
    double xn = 0; //pure number.
    double Re = 0; //pure number.
    double Im = 0; //pure number.
    double xn_cos = 0; //pure number.
    double xn_sin = 0; //pure number.
    double testAmpl = 0; // pure number.
    double testPhase = 0; // rad and then pirad.   

    //Sampling frequency and samples of segment.
    int samplFreq = 50000; // sample/second.
    int N = 3000; //sample.

    // Existing sinusoid.
    double ampl_1 = 100000; //pure number.
    double existFreq_1 = 256; // Hz.
    double phase_1 = 0.67 * pi; // rad.
    double unitAngle_1 = (existFreq_1 / samplFreq) * (2 * pi); // rad/sample.

    // Testing grid.
    double testFreq = 0; // Hz.
    double testUnitAngle = 0; // rad.
    double testAngle = 0; // 
    double lowTestFreq = 0; //Hz.
    double testFreqStep = 1; // Hz.
    int M = 1000; // pure number.
    int m = 0; // pure number.

    for (m = 0; m <= M; ++m)
    {
        testFreq = lowTestFreq + (m * testFreqStep); // Hz.
        testUnitAngle = (testFreq / samplFreq) * (2 * pi); // rad/sample.

        Re = 0; Im = 0;

        for (n = 1; n <= N; ++n)
        {
            xn = ampl_1 * sin(n * unitAngle_1 + phase_1);

            testAngle = n * testUnitAngle;
            xn_cos = xn *  cos(testAngle);
            xn_sin = xn * -sin(testAngle);

            Re += xn_cos;
            Im += xn_sin;
        }  
        std::cout << "Test Freq " << testFreq; // Hz.
        testAmpl = sqrt(Re * Re + Im * Im) / N * 2; //pure number.       
        std::cout << "       Test Freq's Ampl: " << testAmpl;
        testPhase = atan2 (Im, Re); // rad.
        testPhase = testPhase / pi; // pirad.
        testPhase = testPhase + 0.5; // pirad. Corrected phase.
        std::cout << ",    and its phase: " << testPhase 
            << " pirad." << std::endl;
    }       
    return 0;
}

Ampl plot

Phase plot

$\endgroup$
4
  • 4
    $\begingroup$ Hi! I took the output of your program and plotted it. Hope you like the plots, and I hope they help the question! $\endgroup$ Mar 4, 2017 at 16:57
  • $\begingroup$ en.m.wikipedia.org/wiki/Spectral_leakage $\endgroup$
    – Jason R
    Mar 5, 2017 at 1:16
  • $\begingroup$ @ Marcus Müller. Please accept mu many thanks for your plots. I like them very much. Now I understand ups and downs are expected DFT leakage. I have not read yet about leakage in the book of my dear Teacher Mr Lyons. He teaches me by his book though does not respond my messages. So I address my questions here. Regards. $\endgroup$ Mar 6, 2017 at 12:18
  • $\begingroup$ @ Jason R. Please accept my many thanks for the helpful address. Plot there is same as Mr. Marcus Müller's. So it is clear ups and downs are leakage. Wikipedia is too rigorous for me. I hope read about and learn from my text book "understand DSP" by Mr. Richard Lyons. Regards. $\endgroup$ Mar 6, 2017 at 12:24

1 Answer 1

1
$\begingroup$

A DFT includes an implicit finite length window on any function (which has an infinite domain in the case of a sinusoid). The effect of multiplication by that window in the time domain on the DFT result is a convolution with the transform of that window. In the case of a rectangular window, that looks like something very close to a Sinc function (except near DC or Fs/2), or sin(w)/w (which has lots of "ups and downs".)

$\endgroup$
1
  • $\begingroup$ @ hotpaw2. Please accept my many thanks for you answered my question. My understanding is that it is reasonable DFT leakage, as it shown by Mr. Marcus Müller's plot and plot in the address posted by Mr. Jason R. Regards. $\endgroup$ Mar 6, 2017 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.