0
$\begingroup$

I'm trying to plot a frequency-magnitude spectrum, I have 2048 samples of a complex sine wave where the imaginary part is always 0, this results a "mirrored" look, so if I have one sine wave of a 25Hz frequency you will see it twice is the graph:

enter image description here

I saw online the solution to this is to "cut" half of the spectrum so the max frequency would be 50, and I think this would work but I'm having too many problems doing such simple thing, I simply tried to resize both frequency array and magnitude array by half so it would be 1024, in theory it should work but if I do this I get this odd result:

enter image description here

I'm not sure what to try already so if you have any suggestions I'll be happy to hear them, here is my code:

typedef std::complex<float> Complex;
const int N = 2048; // Samples
int main(int argc, char** argv)
{
    vector<float> magnitude(N);
    Complex chunk[N];

    float Fs = 100; // How many time points are needed i,e., Sampling Frequency
    const double  T = 1 / Fs; // At what intervals time points are sampled
    float f = 25; // Frequency
    for (int i = 0; i < N; i++)
    {
        chunk[i] = { (float)(0.7 * cos(2 * M_PI * f * (i * T))), 0 };// generate (complex) sine waveform
        
    }
    CArray data(chunk, N);
    fft(data); // Now FFT results is in data
    int temp = N/2;
    for (int i = 0; i < N; i++)
    {
        magnitude[i] = abs(data[i]);
    }
    float resolution_freq = ((float)Fs / N);
    vector<float> freq_vector = arange(0, Fs, resolution_freq);
    freq_vector.resize(freq_vector.size()-temp);
    magnitude.resize(magnitude.size()-temp);
    QChartView *chartView = plot_freq_magnitude_spectrum(freq_vector,magnitude);
}
$\endgroup$

2 Answers 2

1
$\begingroup$

You are seeing the periodic nature of discrete signals in the frequency domain. For example, a 25 Hz sinusoid sampled at 100 samples/second would have discrete time frequency components at -0.25, 0.25, 0.75, 1.25, 1.75 cycles/sample, and so on forever. This is represented in the DTFT of a cosine:

$$\pi \sum_{k=-\infty}^{\infty} [\delta(\omega - \omega_0 - 2 \pi k) + \delta(\omega - \omega_0 - 2 \pi k)]$$

Where the sum over all k represents the repeated delta functions. We only need to look at one period of the Fourier Transform in order to get all the information we need out of it. In units of Hz, this is usually either from 0 to Fs (your case) or from -Fs/2 to Fs/2.

Further, real signals like the cosine function have even Fourier Transforms. This is the “mirror” effect you are seeing. In this case, all the information in the Fourier Transform is found from 0 to Fs/2. You can normalize your plot this way by how you copy the FFT data to your magnitude array and how you generate your frequency vector.

for (int i = 0; i < N/2; i++)
{
    magnitude[i] = abs(data[i]);
}

and

vector<float> freq_vector = arange(0, Fs/2, resolution_freq);

EDIT: Please forgive my lack of MathJax skills on my phone.

$\endgroup$
0
$\begingroup$

As pointed out earlier by @Ryan, you are observing the periodic nature of sampled/bandlimited signals. Another way to see this is to use fftshift (it looks like you are on Matlab) on the output of your fft. In that case, you will observe the center frequency 0Hz, in the middle. It will look exactly like your plot, the only change should be the x-axis which will go from -50 to 50. In that case, you will see two spikes, one at -25 and the other at 25.

Now, if we go back to your assumption (that you only have a real signal i.e, Real{$e^{j2\pi f n T}$}=$cos(2\pi f n T)$, then you can take a look at the following equality

$$ cos(2\pi f n T) = \frac{e^{j2 \pi f n T}+e^{-j2 \pi f n T}}{2} $$

Here, you see that there is one signal with a frequency of $f$ and another one with a frequency of $-f$ (i.e, since frequency is the rate of phase change, the two exponentials have the same rate of phase change with different directions).

$\endgroup$

Your Answer

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

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