绘制频谱图
Plotting the spectrogram
使用这个 link 的答案:Spectrogram C++ library 我编写了一个代码来计算正弦信号的频谱图:
1-创建正弦信号。
2- 我应用了 Hann Window。
3- 使用 FFTW 。
4- 计算频率系数的对数幅度。
这是脚本:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <fftw3.h>
#include <iostream>
#include <cmath>
using namespace std;
int main(void)
{
int i;
double y;
int N=256;
double Fs=30000;//sampling frequency
double T=1/Fs;//sample time
double f=5000;//frequency
double *in;
fftw_complex *out;
double t[N-1];//time vector
fftw_plan plan_forward;
in = (double*) fftw_malloc(sizeof(double) * N);
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
for (int i=0; i< N;i++)
{
t[i]=i*T;
in[i] =0.7 *sin(2*M_PI*f*t[i]);// generate sine waveform
double multiplier = 0.5 * (1 - cos(2*M_PI*i/(N-1)));//Hanning Window
in[i] = multiplier * in[i];
}
plan_forward = fftw_plan_dft_r2c_1d ( N, in, out, FFTW_ESTIMATE );
printf ( "\n" );
printf ( " Input Data:\n" );
printf ( "\n" );
for ( i = 0; i < N; i++ )
{
printf ( " %4d %12f\n", i, in[i] );
}
fftw_execute ( plan_forward );
printf ( "\n" );
printf ( " log magnitude of frequency domain components :\n" );
printf ( "\n" );
for ( i = 0; i < N; i++ )
{
cout << log(sqrt(out[i][0]*out[i][0]+ out[i][1]*out[i][1])) ;
}
fftw_destroy_plan ( plan_forward );
fftw_free ( in );
fftw_free ( out );
return 0;
}
问题是我应该如何从这里开始?我应该使用哪个库来绘制频谱图?有什么建议么?谢谢
如果您想继续使用 C++ 并实现合理的跨平台,您可能希望将 Qt 视为 UI,将 Qwt 或 QCustomPlot 作为科学绘图小部件
链接
使用这个 link 的答案:Spectrogram C++ library 我编写了一个代码来计算正弦信号的频谱图: 1-创建正弦信号。 2- 我应用了 Hann Window。 3- 使用 FFTW 。 4- 计算频率系数的对数幅度。 这是脚本:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <fftw3.h>
#include <iostream>
#include <cmath>
using namespace std;
int main(void)
{
int i;
double y;
int N=256;
double Fs=30000;//sampling frequency
double T=1/Fs;//sample time
double f=5000;//frequency
double *in;
fftw_complex *out;
double t[N-1];//time vector
fftw_plan plan_forward;
in = (double*) fftw_malloc(sizeof(double) * N);
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
for (int i=0; i< N;i++)
{
t[i]=i*T;
in[i] =0.7 *sin(2*M_PI*f*t[i]);// generate sine waveform
double multiplier = 0.5 * (1 - cos(2*M_PI*i/(N-1)));//Hanning Window
in[i] = multiplier * in[i];
}
plan_forward = fftw_plan_dft_r2c_1d ( N, in, out, FFTW_ESTIMATE );
printf ( "\n" );
printf ( " Input Data:\n" );
printf ( "\n" );
for ( i = 0; i < N; i++ )
{
printf ( " %4d %12f\n", i, in[i] );
}
fftw_execute ( plan_forward );
printf ( "\n" );
printf ( " log magnitude of frequency domain components :\n" );
printf ( "\n" );
for ( i = 0; i < N; i++ )
{
cout << log(sqrt(out[i][0]*out[i][0]+ out[i][1]*out[i][1])) ;
}
fftw_destroy_plan ( plan_forward );
fftw_free ( in );
fftw_free ( out );
return 0;
}
问题是我应该如何从这里开始?我应该使用哪个库来绘制频谱图?有什么建议么?谢谢
如果您想继续使用 C++ 并实现合理的跨平台,您可能希望将 Qt 视为 UI,将 Qwt 或 QCustomPlot 作为科学绘图小部件
链接