How to plot spectrum using FFTW3/ QWT?

2019-08-09 08:13发布

问题:

I want to plot the frequency spectrum (like they do for example in Audacity). Hence I want the frequency in Hertz on the x-axis and the amplitude on the y-axis. My input is a periodically sine wave with 0,7 as amplitude and 500HZ as frequency.I use FFTW to compute the magnitude and QWT to plot. My problem , what parameters should I put in setSamples to get a pic on 500 HZ ? Any help would be appreciated here is my code

回答1:

From documentation you must set two pointers to data that build the curve and the size (number of samples in your case).

void QwtPlotCurve::setSamples(const double *xData, const double *yData, int size)

In your case yData is the data from FFT, the xData is an array that sweep from min to max/2 frequency values of the FFT discarding the other half when the signal is a real signal:

curve->setSamples(signalx, magnitude, N/2);

this should work if I'm correct.

EDIT

Change also:

signalx[i]=i;
magnitude[i]=sqrt(reout[i]*reout[i] + imgout[i]*imgout[i]); //calculate magnitude 

to:

signalx[i]=(double)(Fs * i) / (double)N;
magnitude[i]=sqrt(reout[i]*reout[i] + imgout[i]*imgout[i]) / ((double)N / 2.0);

to represent the frequency for magnitude[i] element in plot.

You should check also for the nyquist frequency. Sampling 500Hz sine wave at 1000Hz produce aliasing and if you plot your signal[] data (at discrete steps) you can see it.



标签: c++ plot fft fftw qwt