类似与kiss_fft的调用,本章具体使用kiss_fftr接口做FFT与iFFT的使用
代码举例
static kiss_fft_scalar rand_scalar(void) {
kiss_fft_scalar s = (kiss_fft_scalar) ((rand() + 10) % 256);
return s / 256.;
}
static void print_fft_result(kiss_fft_cpx *x, int n) {
int l = 0;
for (int i = 0; i < n; i++) {
printf("(%4.4f+%4.4fi), ", x[i].r, x[i].i);
if (l++ == 7) {
l = 0;
printf("\n");
}
}
printf("\n");
}
void test_fftr() {
int i = 0;
int nfft = 20;
kiss_fftr_cfg kiss_fftr_state;
kiss_fft_scalar rin[nfft];
kiss_fft_scalar rout[nfft];
kiss_fft_cpx out[nfft];
memset(rin, 0, sizeof(short) * nfft);
memset(rout, 0, sizeof(short) * nfft);
memset(out, 0, sizeof(kiss_fft_cpx) * nfft);
kiss_fft_scalar zero;
memset(&zero, 0, sizeof(zero));
for (i = 0; i < nfft; ++i) {
rin[i] = rand_scalar();
}
printf(" init data for kiss_fft (rin): \n");
for (i = 0; i < nfft; i++) {
printf("%4.4f,", rin[i]);
}
printf("\n");
kiss_fftr_state = kiss_fftr_alloc(nfft, 0, 0, 0);
kiss_fftr(kiss_fftr_state, rin, out);
kiss_fftr_free(kiss_fftr_state);
printf(" results from kiss_fft (out): \n");
print_fft_result(out, nfft);
kiss_fftr_state = kiss_fftr_alloc(nfft, 1, 0, 0);
for (i = 0; i < nfft; i++) {
out[i].r /= nfft;
out[i].i /= nfft;
}
// case A.
kiss_fftri(kiss_fftr_state, out, rout);
// end case
kiss_fftr_free(kiss_fftr_state);
printf(" results from kiss_ifft (rout): \n");
for (i = 0; i < nfft; i++) {
printf("%4.4f,", rout[i]);
}
printf("\n");
}
int main(int argc, char *const argv[]) {
test_fftr();
return 0;
}