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:
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:
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);
}