diff procedures.h @ 1:6422640a802f

first upload
author Wen X <xue.wen@elec.qmul.ac.uk>
date Tue, 05 Oct 2010 10:45:57 +0100
parents
children 5f3c32dc6e17
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/procedures.h	Tue Oct 05 10:45:57 2010 +0100
@@ -0,0 +1,236 @@
+#ifndef proceduresH
+#define proceduresH
+
+/*
+  procedures.cpp - this file collects miscellaneous structures and functions. Not all of these are
+  referenced somewhere else.
+*/
+
+
+//---------------------------------------------------------------------------
+#include <memory.h>
+#include <stdlib.h>
+#include <values.h>
+#include "fft.h"
+#include "windowfunctions.h"
+
+/*
+  macro testnn: non-negative test. This macro throws out an exception if the value of x is negative.
+*/
+#ifndef testnn
+#define testnn(x) {try{if (x<0) throw("testnn");}catch(...){x=0;}}
+#endif
+
+/*
+  macro AllocateGMM and ReleaseGMM: allocates and frees buffers that hosts descriptors of a Gaussian-
+  mixture model.
+*/
+#define AllocateGMM(_MIXS, _DIM, _P, _M, _DEV) \
+  double* _P=new double[_MIXS]; double** _M=new double*[_MIXS]; double ** _DEV=new double*[_MIXS]; \
+  _M[0]=new double[(_MIXS)*(_DIM)]; _DEV[0]=new double[(_MIXS)*(_DIM)]; \
+  for (int _MIX=1; _MIX<_MIXS; _MIX++) _M[_MIX]=&_M[0][_MIX*(_DIM)], _DEV[_MIX]=&_DEV[0][_MIX*(_DIM)];
+#define ReleaseGMM(_P, _M, _DEV) \
+  delete[] _P; if (_M) {delete[] _M[0]; delete[] _M;} if (_DEV) {delete[] _DEV[0]; delete[] _DEV;}
+
+
+//---------------------------------------------------------------------------
+/*
+  Tick count tool (stop watch) is made up of a TickClock struct and three macros that uses this
+  structure as augument.
+
+  Use of TickClock: declare the instance directly. Use StartClock() and StopClock() to start and stop
+  tick counting. Each time the clock is stopped the interval between last start and stop operations is
+  added to run-time t. Clear the recorded run-time with ResetClock().
+*/
+struct TickClock  //tick counter struct
+{
+  double t;       //accumulated run time
+  __int64 Ticks;  //tick count recorded at start trigger
+  __int64 Ticks2; //tick count recorded at stop trigger
+};
+#define ResetClock(AClock) {AClock.t=0;}
+#define StartClock(AClock) {AClock.Ticks=GetTickCount();}
+#define StopClock(AClock) {AClock.Ticks2=GetTickCount(); AClock.t+=0.001*(AClock.Ticks2-AClock.Ticks);}
+
+//---------------------------------------------------------------------------
+
+struct fftparams  //buffers used by FFT routines
+{
+  double* Amp;    //amplitude output buffer
+  double* Arg;    //phase angle output buffer
+  cdouble* w;     //twiddle factor buffer
+  cdouble* x;     //data buffer
+};
+
+struct TGMM        //Gaussian mixture model
+{
+public:
+  bool mcs;
+  int mixs;       //number of mixtures
+  int dim;        //data dimension
+  double* p;      //mixture weights
+  double** m;     //mixture component centres
+  double** dev;   //diagonal mixture component correlation
+  double acct;
+  TGMM();
+  ~TGMM();
+};
+
+struct TTFSpan  //a rectanguar area in the T-F plane
+{
+  int T1;       //start time
+  int T2;       //finish time
+  double F1;    //lowest frequency
+  double F2;    //highest frequency
+};
+
+struct TSpecPeak  //spectral peak
+{
+  int t;          //time in samples
+  int tres;       //time resolution
+  double f;       //digital frequency
+  double fres;    //frequency resolution
+  double df;      //derivative of f
+  double am;      //amplitude
+  double ph;      //phase angle
+};
+
+/*
+  TSpecTrack is a class that maintains a list of TSpecPeak objects that form a spectral track. It models
+  a sinusid track in sinusoid modeling.
+*/
+class TSpecTrack
+{
+private:
+  int Capacity;     //number of peaks the current storage space is allocated to host
+public:
+  int t1;           //time of first peak
+  int t2;           //time of last peak
+  double fmin;      //minimum peak frequency
+  double fmax;      //maximum peak frequency
+
+  int Count;        //number of peaks in the track
+  TSpecPeak* Peaks; //peaks in the track, ordered by time
+
+  TSpecTrack(int ACapacity=50);
+  ~TSpecTrack();
+
+  void InsertPeak(TSpecPeak& APeak, int index);
+  int LocatePeak(TSpecPeak& APeak);
+  int Add(TSpecPeak& APeak);
+};
+
+/*
+  TTFSpans is a class that maintains a list of TFSpans. This is used to mark selected areas in the time-
+  frequency plane for further processing.
+*/
+class TTFSpans
+{
+public:
+  int Count;      //number of spans
+  int Capacity;   //number of spans the current storage space is allocated to host
+  TTFSpan* Items; //the hosted spans
+
+  TTFSpans();
+  ~TTFSpans();
+  void Add(TTFSpan& ATFSpan);
+  void Clear();
+  int Delete(int Index);
+};
+
+double ACPower(double* data, int Count, void*);
+void Add(double* dest, double* source, int Count);
+void Add(double* out, double* addend, double* adder, int count);
+void ApplyWindow(double* OutBuffer, double* Buffer, double* Weights, int Size);
+double Avg(double*, int, void*);
+void AvgFilter(double* dataout, double* data, int Count, int HWid);
+int BitInv(int value, int order);
+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);
+void Conv(double* out, int N1, double* in1, int N2, double* in2);
+void DCT( double* output, double* input, int N);
+void IDCT( double* output, double* input, int N);
+double DCAmplitude(double*, int, void*);
+double DCPower(double*, int, void*);
+double ddIPHann(double, void*);
+void DeDC(double* data, int Count, int HWid);
+void DeDC_static(double* data, int Count);
+void DoubleToInt(void* out, int BytesPerSample, double* in, int Count);
+void DoubleToIntAdd(void* out, int BytesPerSample, double* in, int Count);
+double DPower(double* data, int Count, void*);
+double Energy(double* data, int Count);
+double ExpOnsetFilter(double* dataout, double* data, int Count, double Tr, double Ta);
+double ExpOnsetFilter_balanced(double* dataout, double* data, int Count, double Tr, double Ta, int bal=5);
+double ExtractLinearComponent(double* dataout, double* data, int Count);
+void FFTConv(double* dest, double* source1, int size1, double* source2, int size2, int zero=0, double* pre_buffer=NULL, double* post_buffer=NULL);
+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);
+void FFTConv(double* dest, double* source1, int size1, double* source2, int size2, double* pre_buffer=NULL);
+void FFTFilter(double* dataout, double* data, int Count, int Wid, int On, int Off);
+void FFTFilterOLA(double* dataout, double* data, int Count, int Wid, int On, int Off, double* pre_buffer=NULL);
+void FFTFilterOLA(unsigned char* dataout, unsigned char* data, int BytesPerSample, int Count, int Wid, int On, int Off, unsigned char* pre_buffer=NULL);
+void FFTFilterOLA(double* dataout, double* data, int Count, double* amp, double* ph, int Wid, double* pre_buffer=NULL);
+double FFTMask(double* dataout, double* data, int Count, int Wid, double DigiOn, double DigiOff, double maskcoef=1);
+int FindInc(double value, double* data, int Count);
+double Gaussian(int Dim, double* Vector, double* Mean, double* Dev, bool StartFrom1);
+void HalfDCT(double* data, int Count, double* CC, int order);
+double Hamming(double f, double T);
+double Hann(double x, double N);
+double HannSq(double x, double N);
+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);
+double I0(double x);
+int InsertDec(double value, double* data, int Count);
+int InsertDec(int value, int* data, int Count);
+int InsertDec(double value, int index, double* data, int* indices, int Count);
+int InsertInc(void* value, void** data, int Count, int (*Compare)(void*, void*));
+int InsertInc(double value, double* data, int Count, bool doinsert=true);
+int InsertInc(double value, int index, double* data, int* indices, int Count);
+int InsertInc(double value, double index, double* data, double* indices, int Count);
+int InsertInc(double value, int* data, int Count, bool doinsert=true);
+int InsertIncApp(double value, double* data, int Count);
+double InstantFreq(int k, int hwid, cdouble* x, int mode=1);
+void InstantFreq(double* freqspec, int hwid, cdouble* x, int mode=1);
+void IntToDouble(double* out, void* in, int BytesPerSample, int Count);
+double IPHannC(double f, cdouble* x, int N, int K1, int K2);
+double IPHannC(double f, void* params);
+double IPHannCP(double f, void* params);
+void lse(double* x, double* y, int Count, double& a, double& b);
+void memdoubleadd(double* dest, double* source, int count);
+void MFCC(int FrameWidth, int NumBands, int Ceps_Order, double* Data, double* Bands, double* C, double* Amps, cdouble* W, cdouble* X);
+double* MFCCPrepareBands(int NumberOfBands, int SamplesPerSec, int NumberOfBins);
+void Multi(double* data, int count, double mul);
+void Multi(double* out, double* in, int count, double mul);
+void MultiAdd(double* out, double* in, double* adder, int count, double mul);
+int NearestPeak(double* data, int count, int anindex);
+double NegativeExp(double* x, double* y, int Count, double& lmd, int sample=1, double step=0.05, double eps=1e-6, int maxiter=50);
+double NL(double* data, int Count, int Wid);
+double Normalize(double* data, int Count, double Maxi=1);
+double Normalize2(double* data, int Count, double Norm=1);
+double nextdouble(double x, double dir);
+double PhaseSpan(double* data, int Count, void*);
+void PolyFit(int P, double* a, int N, double* x, double* y);
+void Pow(double* data, int Count, double ex);
+//int prevcand(candid** cands, int p, int n, int ap);
+int Rectify(double* data, int Count, double th=0);
+double Res(double in, double mod);
+double Romberg(int n, double(*f)(double, void*), double a, double b, void* params=0);
+double Romberg(double(*f)(double, void*), double a, double b, void* params=0, int maxiter=50, double ep=1e-6);
+double sinca(double x);
+double sincd_unn(double x, int N);
+double SmoothPhase(double* Arg, int Count, int mpi=2);
+//int SortCandid(candid cand, candid* cands, int newN);
+double StiffB(double f0, double fm, int m);
+double StiffFm(double f0, int m, double B);
+double StiffF0(double fm, int m, double B);
+double StiffM(double f0, double fm, double B);
+void TFFilter(double* data, double* dataout, int Count, int Wid, TTFSpans* Spans, bool Pass, WindowType wt, double windp, int Sps, int TOffst=0);
+void TFFilter(void* data, void* dataout, int BytesPerSample, int Count, int Wid, TTFSpans* Spans, bool Pass, WindowType wt, double windp, int Sps, int TOffst);
+void TFMask(double* data, double* dataout, int Count, int Wid, TTFSpans* Spans, double masklevel, WindowType wt, double windp, int Sps, int TOffst);
+void TFMask(void* data, void* dataout, int BytesPerSec, int Count, int Wid, TTFSpans* Spans, double masklevel, WindowType wt, double windp, int Sps, int TOffst);
+//double dIPHannC(double f, void* params);
+//void ddsIPWindowStiff(double& df0f0, double& df0B, double& dBB, double& df0, double& dB, double& y, double f0, double B, cdouble* x, int N, int M, double* c, double iH2, double hB, int maxp);
+//void dsIPWindowStiff(double& df0, double& dB, double& y, double f0, double B, cdouble* x, int N, int M, double* c, double iH2, double hB, int maxp);
+//double IPWindowStiff(double f0, double B, cdouble* x, int N, int M, double* c, double iH2, double* ffp, double* vfp, double* pfp, double hB, int maxp);
+//double sIPWindowStiff(double f0, double B, cdouble* x, int N, int M, double* c, double iH2, double* ffp, double* vfp, double* pfp, double hB, int maxp);
+//double sIPWindowStiff(double f0, double B, cdouble* x, int N, int M, double* c, double iH2, double hB, int maxp);
+//double IPWindowMulti(double* f, int ss, cdouble* x, int N, int M, double* c, double iH2, int K1, int K2);
+
+#endif