1
$\begingroup$

I'm having a problem where I cannot explain why the amplitude in the frequency domain not is as expected.

The scenario:

I have a time domain signal of consisting of two sine waves. on at 5Hz with amplitude 2 and one at 10Hz with amplitude 2. the signal is 10 seconds long. The 5 Hz sine wave is only present the first 5 seconds, and the 10Hz sine is only present in the last 5 seconds.

if I take an FFT of the first 5 seconds the magnitude in the frequency domain at 5Hz is as expected. And again if I take an FFT of the last 5 seconds the magnitude of the last 5 seconds is as expected.

But if I take the FFT of the entire signal both the magnitude at 5 and 10 Hz seems to have halfed in magnitude ? Is this expected or am I doing something wrong.

I'm using MathDotNet in a custom made application.

$\endgroup$
  • $\begingroup$ Yes, this is expected. An FFT procedure basically compares a signal with a series of basis sine and cosine waves, so in regions where a particular frequency is not present you should get 0 energy (or close to 0) for that frequency. This will contribute to the total energy, as expected. $\endgroup$ – dsp_user Mar 7 '18 at 12:51
  • $\begingroup$ Ok? but why is this the case? I would like to have a "smoking gun" that I can point at and say that this is the expected behavior because of ....? $\endgroup$ – Jeppe Clausen Mar 7 '18 at 13:07
  • $\begingroup$ I can only refer you to dspguide.com/ch8.htm. $\endgroup$ – dsp_user Mar 7 '18 at 13:15
1
$\begingroup$

If you are seeing half the magnitude (amplitude is the height of the signal, magnitude is the size of a complex value), that means you are using a 1/N normalized DFT (BTW, my preference). The sum will be the same (since the other half is orthogonal), you are just dividing it by twice as large of a number. You should also find that all the even numbered bins are zero and the odd bins have values. This is because the even bin sinusoids split evenly on your half way mark and the odd ones are integer and a half and therefore no longer orthogonal on those regions with your signal.

Hope this helps,

Ced


Followup

Demo for MBaz:

The output is:

Full: -0.000000 + i * 0.000000
Half: 230.265249 + i * 97.354586
Half Magnitude = 250.000000
Half Phase     = 0.400000

The code is:

#include <math.h>
#include <stdio.h>

//===============================================
int main( int argCount, char* argValues[] )
{

        double theSignal[1000];

        double theReal[1000];
        double theImag[1000];

        double theAngleFactor = 2.0 * M_PI / 1000;

        double theFrequency = 50.0;  // Cycles per frame

//--- Build the Arrays

        double thePhi = 0.4;

        for( int n = 0; n < 1000; n++ )
        {
            double theAngle = (double) n
                            * theAngleFactor;

            double theArg = theFrequency * theAngle;

            if( n < 500 )
            {
                theSignal[n] = cos( theArg + thePhi );
            }
            else
            {
                theSignal[n] = -cos( theArg + thePhi );
            }

            theReal[n] =  cos( theArg );
            theImag[n] = -sin( theArg );

        }

//--- Calculate the Bin Value

        double theBinReal = 0.0;
        double theBinImag = 0.0;

        for( int n = 0; n < 1000; n++ )
        {
            theBinReal += theSignal[n] * theReal[n];
            theBinImag += theSignal[n] * theImag[n];
        }

//--- Calculate for Half the Interval

        double theHalfReal = 0.0;
        double theHalfImag = 0.0;

        for( int n = 0; n < 500; n++ )
        {
            theHalfReal += theSignal[n] * theReal[n];
            theHalfImag += theSignal[n] * theImag[n];
        }

        double theMag = sqrt( theHalfReal * theHalfReal
                            + theHalfImag * theHalfImag );

        double thePhase = atan2( theHalfImag, theHalfReal );

//--- Show the Results

        printf( "Full: %f + i * %f\n" ,
                theBinReal, theBinImag );

        printf( "Half: %f + i * %f\n" ,
                theHalfReal, theHalfImag );

        printf( "Half Magnitude = %f\n", theMag );
        printf( "Half Phase     = %f\n", thePhase );
}
//===============================================

Appendum

To elaborate on my original answer. I changed the code so the second half is defined like this, e.g. 10Hz:

                theSignal[n] = cos( 2.0 * theArg + thePhi );

Here are the sums:

Full: 230.265249 + i * 97.354586
Half: 230.265249 + i * 97.354586
Half Magnitude = 250.000000
Half Phase     = 0.400000

I left the amplitude at 1 so the magnitude of the first half unnormalized DFT is 250 (=500/2) as expected. The sums are the same.


Epilogue - The smoking gun

Here are the unnormalized DFT calculations for the signal as specified by the OP near the bins of interest.

Bin 50 is the 5Hz bin.

Bin 100 is the 10Hz bin.

Sample rate is 100Hz. N is 1000 for 10 seconds.

For normalized DFTs:

  • If you divide the left half sums by 500 (N for the left half DFT), your magnitude will be 1.0.

  • if you divide the whole sum by 1000 (N for the whole), your magnitude will be 0.5.

Notice that the 5Hz signal doesn't have much impact on the right side, and the 10Hz signal doesn't have much impact on the left. As I've explained previuosly, the large values for the odd bins are due to the basis vectors in those regions being a whole integer plus a half number of cycles.


        L E F T    H A L F      R I G H T  H A L F           W H O L E
Bin       Real       Imag          Real       Imag          Real       Imag
====  =====================   =======================   =====================
 45     66.907      0.000        -7.771     -0.000        59.136      0.000
 46      0.000      0.000         0.000      0.000         0.000      0.000
 47    109.279      0.000        -7.959      0.000       101.320      0.000
 48      0.000     -0.000        -0.000     -0.000        -0.000     -0.000
 49    321.420      0.000        -8.166     -0.000       313.254      0.000
 50      0.000   -500.000         0.000      0.000         0.000   -500.000
 51   -315.264     -0.000        -8.392     -0.000      -323.656     -0.000
 52      0.000      0.000         0.000      0.000         0.000      0.000
 53   -103.118     -0.000        -8.641      0.000      -111.759     -0.000
 54      0.000     -0.000        -0.000     -0.000         0.000     -0.000
 55    -60.736      0.000        -8.915     -0.000       -69.651      0.000


        L E F T    H A L F      R I G H T  H A L F           W H O L E
Bin       Real       Imag          Real       Imag          Real       Imag
====  =====================   =======================   =====================
 95     -4.985      0.000       -65.080      0.000       -70.065      0.000
 96     -0.000     -0.000        -0.000     -0.000        -0.000     -0.000
 97     -4.714     -0.000      -107.504      0.000      -112.218      0.000
 98     -0.000      0.000         0.000     -0.000         0.000     -0.000
 99     -4.467      0.000      -319.694      0.000      -324.161      0.000
100     -0.000     -0.000         0.000   -500.000         0.000   -500.000
101     -4.240     -0.000       316.942     -0.000       312.701     -0.000
102      0.000      0.000        -0.000      0.000        -0.000      0.000
103     -4.033     -0.000       104.751     -0.000       100.718     -0.000
104     -0.000      0.000        -0.000      0.000        -0.000      0.000
105     -3.841     -0.000        62.325     -0.000        58.484     -0.000

$\endgroup$
0
$\begingroup$

The DFT amplitude depends on the signal's average power. The 5-second sinusoid has a fixed energy. Since power is equal to energy over time, when you add a 5-second interval with zero energy, the average power is halved.

$\endgroup$
  • $\begingroup$ He is not adding another 5-second interval with zero energy, he is adding a different tone. $\endgroup$ – Cedron Dawg Mar 7 '18 at 16:34
  • $\begingroup$ @CedronDawg He's adding energy at 10 Hz, but the average power at 5 Hz is being halved. $\endgroup$ – MBaz Mar 7 '18 at 17:46
  • $\begingroup$ Suppose instead that he added another 5Hz signal, one half cycle out of phase, to the second half. The DFT result would indicate zero 5Hz energy. $\endgroup$ – Cedron Dawg Mar 7 '18 at 19:55
  • $\begingroup$ @CedronDawg Have you actually tried it? $\endgroup$ – MBaz Mar 7 '18 at 23:10
  • $\begingroup$ Well I hadn't, but I did. See the followup in my previous answer. $\endgroup$ – Cedron Dawg Mar 8 '18 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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