changeset 54:9699024bb3d0

Before this commit, the OnsetDetectionFunction class used double precision, and the BTrack class used floats. I have now made BTrack use double precision also. This works fine and the only cost of doing this is that we have to perform one array copy into floating point format so that sample rate conversion (which has to be in floating point format) can take place.
author Adam Stark <adamstark@users.noreply.github.com>
date Wed, 22 Jan 2014 02:49:29 +0000
parents 338f5eb29e41
children 5e520f59127f
files modules-and-plug-ins/python-module/btrack_python_module.cpp src/BTrack.cpp src/BTrack.h
diffstat 3 files changed, 77 insertions(+), 74 deletions(-) [+]
line wrap: on
line diff
--- a/modules-and-plug-ins/python-module/btrack_python_module.cpp	Wed Jan 22 01:34:04 2014 +0000
+++ b/modules-and-plug-ins/python-module/btrack_python_module.cpp	Wed Jan 22 02:49:29 2014 +0000
@@ -156,7 +156,8 @@
     double df[numframes];
     double beats[5000];
     int beatnum = 0;
-    float df_val;
+
+    double df_val;
     
     ///////////////////////////////////////////
 	//////// Begin Processing Loop ////////////
@@ -171,7 +172,7 @@
 		
 		df[i] = onset.getDFsample(buffer);
         
-        df_val = (float) (df[i] + 0.0001);
+        df_val = df[i] + 0.0001;
                 
 		b.process(df_val);				// process df sample in beat tracker
 		
@@ -270,14 +271,14 @@
     
     double beats[5000];
     int beatnum = 0;
-    float df_val;
+    double df_val;
     
     ///////////////////////////////////////////
 	//////// Begin Processing Loop ////////////
 	
 	for (long i=0;i < numframes;i++)
 	{		
-        df_val = (float) (data[i] + 0.0001);
+        df_val = data[i] + 0.0001;
         
 		b.process(df_val);				// process df sample in beat tracker
 		
--- a/src/BTrack.cpp	Wed Jan 22 01:34:04 2014 +0000
+++ b/src/BTrack.cpp	Wed Jan 22 02:49:29 2014 +0000
@@ -28,8 +28,8 @@
 //=======================================================================
 BTrack :: BTrack()
 {	
-	float rayparam = 43;
-	float pi = 3.14159265;
+	double rayparam = 43;
+	double pi = 3.14159265;
 	
 	
 	// initialise parameters
@@ -50,7 +50,7 @@
 	// create rayleigh weighting vector
 	for (int n = 0;n < 128;n++)
 	{
-		wv[n] = ((float) n / pow(rayparam,2)) * exp((-1*pow((float)-n,2)) / (2*pow(rayparam,2)));
+		wv[n] = ((double) n / pow(rayparam,2)) * exp((-1*pow((double)-n,2)) / (2*pow(rayparam,2)));
 	}
 	
 	// initialise prev_delta
@@ -59,9 +59,9 @@
 		prev_delta[i] = 1;
 	}
 	
-	float t_mu = 41/2;
-	float m_sig;
-	float x;
+	double t_mu = 41/2;
+	double m_sig;
+	double x;
 	// create tempo transition matrix
 	m_sig = 41/8;
 	for (int i = 0;i < 41;i++)
@@ -92,10 +92,10 @@
 	framesize = fsize;
 	dfbuffer_size = (512*512)/fsize;		// calculate df buffer size
 	
-	bperiod = round(60/((((float) fsize)/44100)*tempo));
+	bperiod = round(60/((((double) fsize)/44100)*tempo));
 	
-	dfbuffer = new float[dfbuffer_size];	// create df_buffer
-	cumscore = new float[dfbuffer_size];	// create cumscore
+	dfbuffer = new double[dfbuffer_size];	// create df_buffer
+	cumscore = new double[dfbuffer_size];	// create cumscore
 	
 	
 	// initialise df_buffer to zeros
@@ -113,7 +113,7 @@
 }
 
 //=======================================================================
-void BTrack :: process(float df_sample)
+void BTrack :: process(double df_sample)
 {	 
 	m0--;
 	beat--;
@@ -149,7 +149,7 @@
 }
 
 //=======================================================================
-void BTrack :: settempo(float tempo)
+void BTrack :: settempo(double tempo)
 {	 
 	
 	/////////// TEMPO INDICATION RESET //////////////////
@@ -181,7 +181,7 @@
 	/////////// CUMULATIVE SCORE ARTIFICAL TEMPO UPDATE //////////////////
 	
 	// calculate new beat period
-	int new_bperiod = (int) round(60/((((float) framesize)/44100)*tempo));
+	int new_bperiod = (int) round(60/((((double) framesize)/44100)*tempo));
 	
 	int bcounter = 1;
 	// initialise df_buffer to zeros
@@ -212,11 +212,11 @@
 	beat = 0;
 	
 	// offbeat is half of new beat period away
-	m0 = (int) round(((float) new_bperiod)/2);
+	m0 = (int) round(((double) new_bperiod)/2);
 }
 
 //=======================================================================
-void BTrack :: fixtempo(float tempo)
+void BTrack :: fixtempo(double tempo)
 {	
 	// firstly make sure tempo is between 80 and 160 bpm..
 	while (tempo > 160)
@@ -256,6 +256,12 @@
 void BTrack :: dfconvert()
 {
 	float output[512];
+    float input[dfbuffer_size];
+    
+    for (int i = 0;i < dfbuffer_size;i++)
+    {
+        input[i] = (float) dfbuffer[i];
+    }
 		
 	double src_ratio = 512.0/((double) dfbuffer_size);
 	int BUFFER_LEN = dfbuffer_size;
@@ -265,7 +271,7 @@
 	//output_len = (int) floor (((double) BUFFER_LEN) * src_ratio) ;
 	output_len = 512;
 	
-	src_data.data_in = dfbuffer;
+	src_data.data_in = input;
 	src_data.input_frames = BUFFER_LEN;
 	
 	src_data.src_ratio = src_ratio;
@@ -277,7 +283,7 @@
 			
 	for (int i = 0;i < output_len;i++)
 	{
-		df512[i] = src_data.data_out[i];
+		df512[i] = (double) src_data.data_out[i];
 	}
 }
 
@@ -303,17 +309,17 @@
 	// calculate tempo observation vector from bperiod observation vector
 	for (int i = 0;i < 41;i++)
 	{
-		t_index = (int) round(p_fact / ((float) ((2*i)+80)));
-		t_index2 = (int) round(p_fact / ((float) ((4*i)+160)));
+		t_index = (int) round(p_fact / ((double) ((2*i)+80)));
+		t_index2 = (int) round(p_fact / ((double) ((4*i)+160)));
 
 		
 		t_obs[i] = rcf[t_index-1] + rcf[t_index2-1];
 	}
 	
 	
-	float maxval;
-	float maxind;
-	float curval;
+	double maxval;
+	double maxind;
+	double curval;
 	
 	// if tempo is fixed then always use a fixed set of tempi as the previous observation probability function
 	if (tempofix == 1)
@@ -357,23 +363,23 @@
 		prev_delta[j] = delta[j];
 	}
 	
-	bperiod = round((60.0*44100.0)/(((2*maxind)+80)*((float) framesize)));
+	bperiod = round((60.0*44100.0)/(((2*maxind)+80)*((double) framesize)));
 	
 	if (bperiod > 0)
 	{
-		est_tempo = 60.0/((((float) framesize) / 44100.0)*bperiod);
+		est_tempo = 60.0/((((double) framesize) / 44100.0)*bperiod);
 	}
 	
 	//cout << bperiod << endl;
 }
 
 //=======================================================================
-void BTrack :: adapt_thresh(float *x,int N)
+void BTrack :: adapt_thresh(double *x,int N)
 {
 	//int N = 512; // length of df
 	int i = 0;
 	int k,t = 0;
-	float x_thresh[N];
+	double x_thresh[N];
 	
 	int p_post = 7;
 	int p_pre = 8;
@@ -434,10 +440,10 @@
 }
 
 //=======================================================================
-void BTrack :: acf_bal(float *df_thresh)
+void BTrack :: acf_bal(double *df_thresh)
 {
 	int l, n = 0;
-	float sum, tmp;
+	double sum, tmp;
 	
 	// for l lags from 0-511
 	for (l = 0;l < 512;l++)
@@ -456,7 +462,7 @@
 }
 
 //=======================================================================
-float BTrack :: mean_array(float *array,int start,int end)
+double BTrack :: mean_array(double *array,int start,int end)
 {
 	int i;
 	double sum = 0;
@@ -480,7 +486,7 @@
 }
 
 //=======================================================================
-void BTrack :: normalise(float *array,int N)
+void BTrack :: normalise(double *array,int N)
 {
 	double sum = 0;
 	
@@ -502,18 +508,18 @@
 }
 
 //=======================================================================
-void BTrack :: updatecumscore(float df_sample)
+void BTrack :: updatecumscore(double df_sample)
 {	 
 	int start, end, winsize;
-	float max;
+	double max;
 	
 	start = dfbuffer_size - round(2*bperiod);
 	end = dfbuffer_size - round(bperiod/2);
 	winsize = end-start+1;
 	
-	float w1[winsize];
-	float v = -2*bperiod;
-	float wcumscore;
+	double w1[winsize];
+	double v = -2*bperiod;
+	double wcumscore;
 	
 	
 	// create window
@@ -557,8 +563,8 @@
 void BTrack :: predictbeat()
 {	 
 	int winsize = (int) bperiod;
-	float fcumscore[dfbuffer_size + winsize];
-	float w2[winsize];
+	double fcumscore[dfbuffer_size + winsize];
+	double w2[winsize];
 	// copy cumscore to first part of fcumscore
 	for (int i = 0;i < dfbuffer_size;i++)
 	{
@@ -566,7 +572,7 @@
 	}
 	
 	// create future window
-	float v = 1;
+	double v = 1;
 	for (int i = 0;i < winsize;i++)
 	{
 		w2[i] = exp((-1*pow((v - (bperiod/2)),2))   /  (2*pow((bperiod/2) ,2)));
@@ -578,7 +584,7 @@
 	int start = dfbuffer_size - round(2*bperiod);
 	int end = dfbuffer_size - round(bperiod/2);
 	int pastwinsize = end-start+1;
-	float w1[pastwinsize];
+	double w1[pastwinsize];
 
 	for (int i = 0;i < pastwinsize;i++)
 	{
@@ -589,9 +595,9 @@
 	
 
 	// calculate future cumulative score
-	float max;
+	double max;
 	int n;
-	float wcumscore;
+	double wcumscore;
 	for (int i = dfbuffer_size;i < (dfbuffer_size+winsize);i++)
 	{
 		start = i - round(2*bperiod);
@@ -631,10 +637,6 @@
 		n++;
 	}
 		
-	
-	// set beat
-	//beat = beat;
-	
 	// set next prediction time
 	m0 = beat+round(bperiod/2);
 	
--- a/src/BTrack.h	Wed Jan 22 01:34:04 2014 +0000
+++ b/src/BTrack.h	Wed Jan 22 02:49:29 2014 +0000
@@ -36,20 +36,20 @@
 	void initialise(int fsize);
     
     /** Add new sample to buffer and apply beat tracking */
-	void process(float df_sample);
+	void process(double df_sample);
    
     /** Set the tempo of the beat tracker */
-	void settempo(float tempo);
+	void settempo(double tempo);
     
     /** fix tempo to roughly around some value */
-	void fixtempo(float tempo);
+	void fixtempo(double tempo);
     
     /** do not fix the tempo anymore */
 	void unfixtempo();
 	
 	int playbeat;
-	float cscoreval;
-	float est_tempo;
+	double cscoreval;
+	double est_tempo;
 			
 private:
     
@@ -57,7 +57,7 @@
 	void dfconvert();
     
     /** update the cumulative score */
-	void updatecumscore(float df_sample);
+	void updatecumscore(double df_sample);
 	
     /** predicts the next beat */
     void predictbeat();
@@ -68,47 +68,47 @@
     /** calculates an adaptive threshold which is used to remove low level energy from detection 
      * function and emphasise peaks 
      */
-	void adapt_thresh(float *x,int N);
+	void adapt_thresh(double *x,int N);
     
     /** calculates the mean of values in an array from index locations [start,end] */
-	float mean_array(float *array,int start,int end);
+	double mean_array(double *array,int start,int end);
     
     /** normalises a given array */
-	void normalise(float *array,int N);
+	void normalise(double *array,int N);
     
     /** calculates the balanced autocorrelation of the smoothed detection function */
-	void acf_bal(float *df_thresh);
+	void acf_bal(double *df_thresh);
     
     /** returns the output of the comb filter */
 	void getrcfoutput();
 	
 	// buffers
-	float *dfbuffer;			/**< to hold detection function */
-	float df512[512];			/**< to hold resampled detection function */
-	float *cumscore;			/**<  to hold cumulative score */
+	double *dfbuffer;			/**< to hold detection function */
+	double df512[512];			/**< to hold resampled detection function */
+	double *cumscore;			/**<  to hold cumulative score */
 	
-	float acf[512];				/**<  to hold autocorrelation function */
+	double acf[512];				/**<  to hold autocorrelation function */
 	
-	float wv[128];				/**<  to hold weighting vector */
+	double wv[128];				/**<  to hold weighting vector */
 	
-	float rcf[128];				/**<  to hold comb filter output */
-	float t_obs[41];			/**<  to hold tempo version of comb filter output */
+	double rcf[128];				/**<  to hold comb filter output */
+	double t_obs[41];			/**<  to hold tempo version of comb filter output */
 	
-	float delta[41];			/**<  to hold final tempo candidate array */
-	float prev_delta[41];		/**<  previous delta */
-	float prev_delta_fix[41];	/**<  fixed tempo version of previous delta */
+	double delta[41];			/**<  to hold final tempo candidate array */
+	double prev_delta[41];		/**<  previous delta */
+	double prev_delta_fix[41];	/**<  fixed tempo version of previous delta */
 	
-	float t_tmat[41][41];		/**<  transition matrix */
+	double t_tmat[41][41];		/**<  transition matrix */
 	
 	
 	// parameters
-	float tightness;
-	float alpha;
-	float bperiod;
-	float tempo;
+	double tightness;
+	double alpha;
+	double bperiod;
+	double tempo;
 	
 	
-	float p_fact;
+	double p_fact;
 	
 	
 	//