procedures.h
Go to the documentation of this file.
1 /*
2  Harmonic sinusoidal modelling and tools
3 
4  C++ code package for harmonic sinusoidal modelling and relevant signal processing.
5  Centre for Digital Music, Queen Mary, University of London.
6  This file copyright 2011 Wen Xue.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12 */
13 #ifndef proceduresH
14 #define proceduresH
15 
22 //---------------------------------------------------------------------------
23 #include <memory.h>
24 #include <stdlib.h>
25 #include <values.h>
26 #include "fft.h"
27 #include "windowfunctions.h"
28 
32 #ifndef testnn
33 #define testnn(x) {try{if (x<0) throw("testnn");}catch(...){x=0;}}
34 #endif
35 
36 /*
37  macro AllocateGMM and ReleaseGMM: allocates and frees buffers that hosts descriptors of a Gaussian-
38  mixture model.
39 */
40 #define AllocateGMM(_MIXS, _DIM, _P, _M, _DEV) \
41  double* _P=new double[_MIXS]; double** _M=new double*[_MIXS]; double ** _DEV=new double*[_MIXS]; \
42  _M[0]=new double[(_MIXS)*(_DIM)]; _DEV[0]=new double[(_MIXS)*(_DIM)]; \
43  for (int _MIX=1; _MIX<_MIXS; _MIX++) _M[_MIX]=&_M[0][_MIX*(_DIM)], _DEV[_MIX]=&_DEV[0][_MIX*(_DIM)];
44 #define ReleaseGMM(_P, _M, _DEV) \
45  delete[] _P; if (_M) {delete[] _M[0]; delete[] _M;} if (_DEV) {delete[] _DEV[0]; delete[] _DEV;}
46 
47 
48 //---------------------------------------------------------------------------
57 struct TickClock //tick counter struct
58 {
59  double t; //accumulated run time
60  __int64 Ticks; //tick count recorded at start trigger
61  __int64 Ticks2; //tick count recorded at stop trigger
62 };
63 #define ResetClock(AClock) {AClock.t=0;}
64 #define StartClock(AClock) {AClock.Ticks=GetTickCount();}
65 #define StopClock(AClock) {AClock.Ticks2=GetTickCount(); AClock.t+=0.001*(AClock.Ticks2-AClock.Ticks);}
66 
67 //---------------------------------------------------------------------------
68 
69 struct fftparams //buffers used by FFT routines
70 {
71  double* Amp; //amplitude output buffer
72  double* Arg; //phase angle output buffer
73  cdouble* w; //twiddle factor buffer
74  cdouble* x; //data buffer
75 };
76 
77 struct TGMM //Gaussian mixture model
78 {
79 public:
80  bool mcs;
81  int mixs; //number of mixtures
82  int dim; //data dimension
83  double* p; //mixture weights
84  double** m; //mixture component centres
85  double** dev; //diagonal mixture component correlation
86  double acct;
87  TGMM();
88  ~TGMM();
89 };
90 
91 struct TTFSpan //a rectanguar area in the T-F plane
92 {
93  int T1; //start time
94  int T2; //finish time
95  double F1; //lowest frequency
96  double F2; //highest frequency
97 };
98 
99 struct TSpecPeak //spectral peak
100 {
101  int t; //time in samples
102  int tres; //time resolution
103  double f; //digital frequency
104  double fres; //frequency resolution
105  double df; //derivative of f
106  double am; //amplitude
107  double ph; //phase angle
108 };
109 
115 {
116 private:
117  int Capacity; //number of peaks the current storage space is allocated to host
118 public:
119  int t1; //time of first peak
120  int t2; //time of last peak
121  double fmin; //minimum peak frequency
122  double fmax; //maximum peak frequency
123 
124  int Count; //number of peaks in the track
125  TSpecPeak* Peaks; //peaks in the track, ordered by time
126 
127  TSpecTrack(int ACapacity=50);
128  ~TSpecTrack();
129 
130  void InsertPeak(TSpecPeak& APeak, int index);
131  int LocatePeak(TSpecPeak& APeak);
132  int Add(TSpecPeak& APeak);
133 };
134 
139 class TTFSpans
140 {
141 public:
142  int Count; //number of spans
143  int Capacity; //number of spans the current storage space is allocated to host
144  TTFSpan* Items; //the hosted spans
145 
146  TTFSpans();
147  ~TTFSpans();
148  void Add(TTFSpan& ATFSpan);
149  void Clear();
150  int Delete(int Index);
151 };
152 
153 double ACPower(double* data, int Count, void*);
154 void Add(double* dest, double* source, int Count);
155 void Add(double* out, double* addend, double* adder, int count);
156 void ApplyWindow(double* OutBuffer, double* Buffer, double* Weights, int Size);
157 double Avg(double*, int, void*);
158 void AvgFilter(double* dataout, double* data, int Count, int HWid);
159 int BitInv(int value, int order);
160 void CalculateSpectrogram(double* data, int Count, int start, int end, int Wid, int Offst, double* Window=0, double** Spec=0, double** Ph=0, double amp=1, bool half=true);
161 void Conv(double* out, int N1, double* in1, int N2, double* in2);
162 void DCT( double* output, double* input, int N);
163 void IDCT( double* output, double* input, int N);
164 double DCAmplitude(double*, int, void*);
165 double DCPower(double*, int, void*);
166 double ddIPHann(double, void*);
167 void DeDC(double* data, int Count, int HWid);
168 void DeDC_static(double* data, int Count);
169 void DoubleToInt(void* out, int BytesPerSample, double* in, int Count);
170 void DoubleToIntAdd(void* out, int BytesPerSample, double* in, int Count);
171 double DPower(double* data, int Count, void*);
172 double Energy(double* data, int Count);
173 double ExpOnsetFilter(double* dataout, double* data, int Count, double Tr, double Ta);
174 double ExpOnsetFilter_balanced(double* dataout, double* data, int Count, double Tr, double Ta, int bal=5);
175 double ExtractLinearComponent(double* dataout, double* data, int Count);
176 void FFTConv(double* dest, double* source1, int size1, double* source2, int size2, int zero=0, double* pre_buffer=NULL, double* post_buffer=NULL);
177 void FFTConv(unsigned char* dest, unsigned char* source1, int bps, int size1, double* source2, int size2, int zero=0, unsigned char* pre_buffer=NULL, unsigned char* post_buffer=NULL);
178 void FFTConv(double* dest, double* source1, int size1, double* source2, int size2, double* pre_buffer=NULL);
179 void FFTFilter(double* dataout, double* data, int Count, int Wid, int On, int Off);
180 void FFTFilterOLA(double* dataout, double* data, int Count, int Wid, int On, int Off, double* pre_buffer=NULL);
181 void FFTFilterOLA(unsigned char* dataout, unsigned char* data, int BytesPerSample, int Count, int Wid, int On, int Off, unsigned char* pre_buffer=NULL);
182 void FFTFilterOLA(double* dataout, double* data, int Count, double* amp, double* ph, int Wid, double* pre_buffer=NULL);
183 double FFTMask(double* dataout, double* data, int Count, int Wid, double DigiOn, double DigiOff, double maskcoef=1);
184 int FindInc(double value, double* data, int Count);
185 double Gaussian(int Dim, double* Vector, double* Mean, double* Dev, bool StartFrom1);
186 void HalfDCT(double* data, int Count, double* CC, int order);
187 double Hamming(double f, double T);
188 double Hann(double x, double N);
189 double HannSq(double x, double N);
190 int HxPeak2(double*& hps, double*& vhps, double (*F)(double, void*), double (*dF)(double, void*), double(*ddF)(double, void*), void* params, double st, double en, double epf=1e-6);
191 double I0(double x);
192 int InsertDec(double value, double* data, int Count);
193 int InsertDec(int value, int* data, int Count);
194 int InsertDec(double value, int index, double* data, int* indices, int Count);
195 int InsertInc(void* value, void** data, int Count, int (*Compare)(void*, void*));
196 int InsertInc(double value, double* data, int Count, bool doinsert=true);
197 int InsertInc(double value, int index, double* data, int* indices, int Count);
198 int InsertInc(double value, double index, double* data, double* indices, int Count);
199 int InsertInc(double value, int* data, int Count, bool doinsert=true);
200 int InsertIncApp(double value, double* data, int Count);
201 double InstantFreq(int k, int hwid, cdouble* x, int mode=1);
202 void InstantFreq(double* freqspec, int hwid, cdouble* x, int mode=1);
203 void IntToDouble(double* out, void* in, int BytesPerSample, int Count);
204 double IPHannC(double f, cdouble* x, int N, int K1, int K2);
205 double IPHannC(double f, void* params);
206 double IPHannCP(double f, void* params);
207 void lse(double* x, double* y, int Count, double& a, double& b);
208 void memdoubleadd(double* dest, double* source, int count);
209 void MFCC(int FrameWidth, int NumBands, int Ceps_Order, double* Data, double* Bands, double* C, double* Amps, cdouble* W, cdouble* X);
210 double* MFCCPrepareBands(int NumberOfBands, int SamplesPerSec, int NumberOfBins);
211 void Multi(double* data, int count, double mul);
212 void Multi(double* out, double* in, int count, double mul);
213 void MultiAdd(double* out, double* in, double* adder, int count, double mul);
214 int NearestPeak(double* data, int count, int anindex);
215 double NegativeExp(double* x, double* y, int Count, double& lmd, int sample=1, double step=0.05, double eps=1e-6, int maxiter=50);
216 double NL(double* data, int Count, int Wid);
217 double Normalize(double* data, int Count, double Maxi=1);
218 double Normalize2(double* data, int Count, double Norm=1);
219 double nextdouble(double x, double dir);
220 double PhaseSpan(double* data, int Count, void*);
221 void PolyFit(int P, double* a, int N, double* x, double* y);
222 void Pow(double* data, int Count, double ex);
223 //int prevcand(candid** cands, int p, int n, int ap);
224 int Rectify(double* data, int Count, double th=0);
225 double Res(double in, double mod);
226 double Romberg(int n, double(*f)(double, void*), double a, double b, void* params=0);
227 double Romberg(double(*f)(double, void*), double a, double b, void* params=0, int maxiter=50, double ep=1e-6);
228 double sinca(double x);
229 double sincd_unn(double x, int N);
230 double SmoothPhase(double* Arg, int Count, int mpi=2);
231 //int SortCandid(candid cand, candid* cands, int newN);
232 double StiffB(double f0, double fm, int m);
233 double StiffFm(double f0, int m, double B);
234 double StiffF0(double fm, int m, double B);
235 double StiffM(double f0, double fm, double B);
236 void TFFilter(double* data, double* dataout, int Count, int Wid, TTFSpans* Spans, bool Pass, WindowType wt, double windp, int Sps, int TOffst=0);
237 void TFFilter(void* data, void* dataout, int BytesPerSample, int Count, int Wid, TTFSpans* Spans, bool Pass, WindowType wt, double windp, int Sps, int TOffst);
238 void TFMask(double* data, double* dataout, int Count, int Wid, TTFSpans* Spans, double masklevel, WindowType wt, double windp, int Sps, int TOffst);
239 void TFMask(void* data, void* dataout, int BytesPerSec, int Count, int Wid, TTFSpans* Spans, double masklevel, WindowType wt, double windp, int Sps, int TOffst);
240 
241 #endif
void DeDC(double *data, int Count, int HWid)
Definition: procedures.cpp:485
void FFTFilter(double *dataout, double *data, int Count, int Wid, int On, int Off)
Definition: procedures.cpp:1051
void memdoubleadd(double *dest, double *source, int count)
Definition: procedures.cpp:2003
void IDCT(double *output, double *input, int N)
Definition: procedures.cpp:464
int HxPeak2(double *&hps, double *&vhps, double(*F)(double, void *), double(*dF)(double, void *), double(*ddF)(double, void *), void *params, double st, double en, double epf=1e-6)
Definition: procedures.cpp:1439
double ExtractLinearComponent(double *dataout, double *data, int Count)
Definition: procedures.cpp:637
void Multi(double *data, int count, double mul)
Definition: procedures.cpp:2120
double * MFCCPrepareBands(int NumberOfBands, int SamplesPerSec, int NumberOfBins)
Definition: procedures.cpp:2090
double PhaseSpan(double *data, int Count, void *)
Definition: procedures.cpp:2312
int FindInc(double value, double *data, int Count)
Definition: procedures.cpp:1272
cdouble * Window(cdouble *x, double f, int N, int M, double *c, int K1, int K2)
Definition: sinest.cpp:102
double Normalize(double *data, int Count, double Maxi=1)
Definition: procedures.cpp:2266
int BitInv(int value, int order)
Definition: fft.cpp:50
Definition: procedures.h:91
Definition: procedures.h:139
double StiffFm(double f0, int m, double B)
Definition: procedures.cpp:2577
double NL(double *data, int Count, int Wid)
Definition: procedures.cpp:2239
void TFFilter(double *data, double *dataout, int Count, int Wid, TTFSpans *Spans, bool Pass, WindowType wt, double windp, int Sps, int TOffst=0)
Definition: procedures.cpp:2633
void IntToDouble(double *out, void *in, int BytesPerSample, int Count)
Definition: quickspec.cpp:116
int InsertIncApp(double value, double *data, int Count)
Definition: procedures.cpp:1848
int Rectify(double *data, int Count, double th=0)
Definition: procedures.cpp:2386
void AvgFilter(double *dataout, double *data, int Count, int HWid)
Definition: procedures.cpp:285
double Hamming(double f, double T)
Definition: procedures.cpp:1318
double sinca(double x)
Definition: procedures.cpp:2505
void DeDC_static(double *data, int Count)
Definition: procedures.cpp:502
void MFCC(int FrameWidth, int NumBands, int Ceps_Order, double *Data, double *Bands, double *C, double *Amps, cdouble *W, cdouble *X)
Definition: procedures.cpp:2048
double Normalize2(double *data, int Count, double Norm=1)
Definition: procedures.cpp:2293
double Avg(double *, int, void *)
Definition: procedures.cpp:266
double sincd_unn(double x, int N)
Definition: procedures.cpp:2519
void ApplyWindow(double *OutBuffer, double *Buffer, double *Weights, int Size)
Definition: procedures.cpp:253
double IPHannC(double f, cdouble *x, int N, int K1, int K2)
Definition: procedures.cpp:1963
void Pow(double *data, int Count, double ex)
Definition: procedures.cpp:2370
void FFTFilterOLA(double *dataout, double *data, int Count, int Wid, int On, int Off, double *pre_buffer=NULL)
Definition: procedures.cpp:1120
Definition: procedures.h:99
double Hann(double x, double N)
Definition: procedures.cpp:1396
double ACPower(double *data, int Count, void *)
Definition: procedures.cpp:201
void DCT(double *output, double *input, int N)
Definition: procedures.cpp:441
double DCAmplitude(double *, int, void *)
Definition: procedures.cpp:399
Definition: procedures.h:77
double InstantFreq(int k, int hwid, cdouble *x, int mode=1)
Definition: procedures.cpp:1888
double StiffB(double f0, double fm, int m)
Definition: procedures.cpp:2560
double ExpOnsetFilter(double *dataout, double *data, int Count, double Tr, double Ta)
Definition: procedures.cpp:589
Definition: procedures.h:57
void MultiAdd(double *out, double *in, double *adder, int count, double mul)
Definition: procedures.cpp:2148
void CalculateSpectrogram(double *data, int Count, int start, int end, int Wid, int Offst, double *Window=0, double **Spec=0, double **Ph=0, double amp=1, bool half=true)
Definition: procedures.cpp:325
double DCPower(double *, int, void *)
Definition: procedures.cpp:419
void PolyFit(int P, double *a, int N, double *x, double *y)
Definition: procedures.cpp:2340
double StiffM(double f0, double fm, double B)
Definition: procedures.cpp:2608
double Res(double in, double mod)
Definition: procedures.cpp:2406
double FFTMask(double *dataout, double *data, int Count, int Wid, double DigiOn, double DigiOff, double maskcoef=1)
Definition: procedures.cpp:1180
double DPower(double *data, int Count, void *)
Definition: procedures.cpp:556
void FFTConv(double *dest, double *source1, int size1, double *source2, int size2, int zero=0, double *pre_buffer=NULL, double *post_buffer=NULL)
Definition: procedures.cpp:671
void DoubleToIntAdd(void *out, int BytesPerSample, double *in, int Count)
Definition: procedures.cpp:534
double Romberg(int n, double(*f)(double, void *), double a, double b, void *params=0)
Definition: procedures.cpp:2426
double HannSq(double x, double N)
Definition: procedures.cpp:1358
Definition: xcomplex.h:26
int NearestPeak(double *data, int count, int anindex)
Definition: procedures.cpp:2163
int InsertDec(int value, int *data, int Count)
Definition: procedures.cpp:1541
Definition: procedures.h:69
void lse(double *x, double *y, int Count, double &a, double &b)
Definition: procedures.cpp:1980
double NegativeExp(double *x, double *y, int Count, double &lmd, int sample=1, double step=0.05, double eps=1e-6, int maxiter=50)
Definition: procedures.cpp:2194
double SmoothPhase(double *Arg, int Count, int mpi=2)
Definition: procedures.cpp:2535
double Energy(double *data, int Count)
Definition: procedures.cpp:571
Definition: procedures.h:114
double ExpOnsetFilter_balanced(double *dataout, double *data, int Count, double Tr, double Ta, int bal=5)
Definition: procedures.cpp:616
int InsertInc(double value, double *data, int Count, bool doinsert=true)
Definition: procedures.cpp:1704
double StiffF0(double fm, int m, double B)
Definition: procedures.cpp:2592
void DoubleToInt(void *out, int BytesPerSample, double *in, int Count)
Definition: procedures.cpp:518
void Add(double *dest, double *source, int Count)
Definition: procedures.cpp:224
void Conv(double *out, int N1, double *in1, int N2, double *in2)
Definition: procedures.cpp:362
double I0(double x)
Definition: windowfunctions.cpp:25