首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有复杂输出的C/C++ Goertzel算法还是magnitude+phase?

具有复杂输出的C/C++ Goertzel算法还是magnitude+phase?
EN

Stack Overflow用户
提问于 2012-01-12 21:26:00
回答 1查看 1.9K关注 0票数 3

有人知道我可以从哪里获得代码或库来执行复杂输出的Goertzel算法吗?(或者其他任何1-bin-DFT算法?)

EN

回答 1

Stack Overflow用户

发布于 2018-04-27 04:36:22

下面是几年前我写的代码。你可以随意使用它,只要有适当的属性。

goertzelfilter.h

代码语言:javascript
复制
/* goertzelfilter.h
*/

#ifndef GOERTZELFILTER_H_
#define GOERTZELFILTER_H_

#include <complex.h>

typedef struct goertzelfilterstruct {
  double coeff ;
  double sine ;
  double cosine ;
} GoertzelFilter;

GoertzelFilter goertzelSetup( double normalizedfreq );

double complex goertzelFilterC( double *sample, int nsamples, GoertzelFilter *g );

#endif

goerzelfilter.c

代码语言:javascript
复制
/* goertzelfilter.c
*/

#include <math.h>
#include <stdlib.h>
#include <complex.h>

#include "goertzelfilter.h"

GoertzelFilter goertzelSetup( double normalizedfreq )
{
  double w = 2*M_PI*normalizedfreq;
  double wr, wi;

  GoertzelFilter g;

  wr = cos(w);
  wi = sin(w);
  g.coeff = 2 * wr;
  g.cosine = wr;
  g.sine = wi;

  return g;
}

double complex goertzelFilterC( double *samples, int nsamples, GoertzelFilter *g )
{
  double sprev = 0.0;
  double sprev2 = 0.0;
  double s, imag, real;
  int n;

  for (n=0; n<nsamples; n++ ) {
    s = samples[n] + g->coeff * sprev - sprev2;
    sprev2 = sprev;
    sprev = s;
  }

  real = sprev*g->cosine - sprev2;
  imag = -sprev*g->sine;

  return real + I*imag;
}

作为测试,你可以试试这个,

代码语言:javascript
复制
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <complex.h>

#include "goertzelfilter.h"

#define LEN(a) (sizeof(a)/sizeof(a[0]) )

int main() {

  // This will hold our input and output data
  double data[1024] = { 0. };
  double complex filtered[1024] = { 0. };

  // This will hold the filter constants
  GoertzelFilter g = { 0. };

  int n;
  int nwindow = 16;

  double f = 4./LEN(data) ;

  // Generate data with noise
  for ( n = 0 ; n < LEN(data) ; n++ ) {
    data[n] = sin( n * (2.*M_PI) * f ) + 0.5*((float)rand()/RAND_MAX - 0.5);
  }

  // Set up the filter constants, note that we choose a frequency
  g = goertzelSetup( f );

  // Filter the data using a sliding window
  for( n = 0 ; n < LEN(data)-nwindow ; n++ ) {
    filtered[n+nwindow/2]  = goertzelFilterC( &data[n], nwindow, &g )/nwindow;
  }

  // Print the real Valued Data (1st column) and complex valued Goertzel output
  for( n = 0 ; n < LEN(data); n++ ) {
    printf( "%g %g %g\n", data[n], creal(filtered[n]), cimag(filtered[n]) );
  }

}

下面的图表显示了测试代码的输入和输出:

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8835806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档