File: fftcalc.cpp

package info (click to toggle)
qsstv 9.5.8-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,928 kB
  • sloc: cpp: 47,579; makefile: 4
file content (74 lines) | stat: -rw-r--r-- 1,695 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "fftcalc.h"
#include "appglobal.h"
#include "logging.h"

#include <string.h>

fftCalc::fftCalc()
{
  plan=NULL;
  out=NULL;
  dataBuffer=NULL;
  dataBufferWindowed=NULL;
}

fftCalc::~fftCalc()
{
  if(plan)fftw_destroy_plan(plan);
  if(out) fftw_free(out);
  if(dataBuffer) fftw_free(dataBuffer);
}

void fftCalc::init(int length,int nblocks,int isamplingrate)
{
  int i;
  windowSize=length;
  fftLength=windowSize*nblocks;
  numBlocks=nblocks;
  blockIndex=0;
  createHamming();
  samplingrate=isamplingrate;
  //prepare fft
  if(plan)fftw_destroy_plan(plan);
  if(out) fftw_free(out);
  if(dataBuffer) delete [] dataBuffer;

  dataBuffer=new double[fftLength];
  for(i=0;i<fftLength;i++)
    {
      dataBuffer[i]=0.;
    }
  if(dataBufferWindowed) fftw_free(dataBufferWindowed);
  out =         (double *)fftw_malloc(fftLength * sizeof(double));
  dataBufferWindowed  = (double *)fftw_malloc(fftLength * sizeof(double));
  // create the fftw plan
  addToLog("fftw_plan fftcalc start",LOGFFT);
  plan = fftw_plan_r2r_1d(fftLength, dataBufferWindowed, out, FFTW_R2HC, FFTW_ESTIMATE);
  addToLog("fftw_plan fftcalc stop",LOGFFT);
}

void fftCalc::createHamming()
{
  int i;
  hammingBuffer= new double[fftLength];
  for(i=0;i<fftLength;i++)
    {
      hammingBuffer[i]=0.54-(0.46*cos(2*M_PI*((double)i/((double)(fftLength-1)))));
    }

}

void fftCalc::realFFT(double *data)
{
  int i;
  memmove(&dataBuffer[0],&dataBuffer[windowSize],sizeof(double)*windowSize*(numBlocks-1));
  memmove(&dataBuffer[windowSize*(numBlocks-1)],data,sizeof(double)*windowSize);
  for(i=0;i<fftLength;i++)
    {
      dataBufferWindowed[i]=dataBuffer[i]*hammingBuffer[i];
    }
  fftw_execute(plan);
}