Changeset fddfa64 for src/pitch/pitchfcomb.c
- Timestamp:
- Nov 3, 2009, 4:14:03 PM (14 years ago)
- Branches:
- feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
- Children:
- bafe71d
- Parents:
- 63f3c70
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/pitch/pitchfcomb.c
r63f3c70 rfddfa64 30 30 #define MAX_PEAKS 8 31 31 32 typedef struct { 32 typedef struct 33 { 33 34 smpl_t bin; 34 35 smpl_t db; 35 36 } aubio_fpeak_t; 36 37 37 struct _aubio_pitchfcomb_t { 38 struct _aubio_pitchfcomb_t 39 { 38 40 uint_t fftSize; 39 41 uint_t stepSize; 40 42 uint_t rate; 41 fvec_t * 42 fvec_t * 43 cvec_t * 44 fvec_t * 45 aubio_fft_t * 43 fvec_t *winput; 44 fvec_t *win; 45 cvec_t *fftOut; 46 fvec_t *fftLastPhase; 47 aubio_fft_t *fft; 46 48 }; 47 49 48 aubio_pitchfcomb_t * new_aubio_pitchfcomb (uint_t bufsize, uint_t hopsize, uint_t channels) 50 aubio_pitchfcomb_t * 51 new_aubio_pitchfcomb (uint_t bufsize, uint_t hopsize, uint_t channels) 49 52 { 50 aubio_pitchfcomb_t * p = AUBIO_NEW(aubio_pitchfcomb_t);51 p->fftSize 52 p->stepSize 53 p->winput = new_fvec(bufsize,1);54 p->fftOut = new_cvec(bufsize,1);55 p->fftLastPhase = new_fvec (bufsize, channels);56 p->fft = new_aubio_fft (bufsize, 1);57 p->win = new_aubio_window ("hanning", bufsize);53 aubio_pitchfcomb_t *p = AUBIO_NEW (aubio_pitchfcomb_t); 54 p->fftSize = bufsize; 55 p->stepSize = hopsize; 56 p->winput = new_fvec (bufsize, 1); 57 p->fftOut = new_cvec (bufsize, 1); 58 p->fftLastPhase = new_fvec (bufsize, channels); 59 p->fft = new_aubio_fft (bufsize, 1); 60 p->win = new_aubio_window ("hanning", bufsize); 58 61 return p; 59 62 } 60 63 61 64 /* input must be stepsize long */ 62 void aubio_pitchfcomb_do (aubio_pitchfcomb_t * p, fvec_t * input, fvec_t * output) 65 void 66 aubio_pitchfcomb_do (aubio_pitchfcomb_t * p, fvec_t * input, fvec_t * output) 63 67 { 64 68 uint_t i, k, l, maxharm = 0; 65 smpl_t phaseDifference = TWO_PI *(smpl_t)p->stepSize/(smpl_t)p->fftSize;69 smpl_t phaseDifference = TWO_PI * (smpl_t) p->stepSize / (smpl_t) p->fftSize; 66 70 aubio_fpeak_t peaks[MAX_PEAKS]; 67 71 68 72 for (i = 0; i < input->channels; i++) { 69 73 70 for (k=0; k<MAX_PEAKS; k++) {71 peaks[k].db = -200.;72 peaks[k].bin = 0.;73 }74 for (k = 0; k < MAX_PEAKS; k++) { 75 peaks[k].db = -200.; 76 peaks[k].bin = 0.; 77 } 74 78 75 for (k=0; k < input->length; k++){76 p->winput->data[0][k] = p->win->data[0][k] * input->data[i][k];77 }78 aubio_fft_do(p->fft,p->winput,p->fftOut);79 for (k = 0; k < input->length; k++) { 80 p->winput->data[0][k] = p->win->data[0][k] * input->data[i][k]; 81 } 82 aubio_fft_do (p->fft, p->winput, p->fftOut); 79 83 80 for (k=0; k<=p->fftSize/2; k++) {81 smpl_t82 magnitude = 20.*LOG10(2.*p->fftOut->norm[0][k]/(smpl_t)p->fftSize),83 phase = p->fftOut->phas[0][k],84 tmp, bin;84 for (k = 0; k <= p->fftSize / 2; k++) { 85 smpl_t 86 magnitude = 87 20. * LOG10 (2. * p->fftOut->norm[0][k] / (smpl_t) p->fftSize), 88 phase = p->fftOut->phas[0][k], tmp, bin; 85 89 86 /* compute phase difference */87 tmp = phase - p->fftLastPhase->data[i][k];88 p->fftLastPhase->data[i][k] = phase;90 /* compute phase difference */ 91 tmp = phase - p->fftLastPhase->data[i][k]; 92 p->fftLastPhase->data[i][k] = phase; 89 93 90 /* subtract expected phase difference */91 tmp -= (smpl_t)k*phaseDifference;94 /* subtract expected phase difference */ 95 tmp -= (smpl_t) k *phaseDifference; 92 96 93 /* map delta phase into +/- Pi interval */94 tmp = aubio_unwrap2pi(tmp);97 /* map delta phase into +/- Pi interval */ 98 tmp = aubio_unwrap2pi (tmp); 95 99 96 /* get deviation from bin frequency from the +/- Pi interval */97 tmp = p->fftSize/(smpl_t)p->stepSize*tmp/(TWO_PI);100 /* get deviation from bin frequency from the +/- Pi interval */ 101 tmp = p->fftSize / (smpl_t) p->stepSize * tmp / (TWO_PI); 98 102 99 /* compute the k-th partials' true bin */100 bin = (smpl_t)k + tmp;103 /* compute the k-th partials' true bin */ 104 bin = (smpl_t) k + tmp; 101 105 102 if (bin > 0.0 && magnitude > peaks[0].db) { // && magnitude < 0) { 103 memmove(peaks+1, peaks, sizeof(aubio_fpeak_t)*(MAX_PEAKS-1)); 104 peaks[0].bin = bin; 105 peaks[0].db = magnitude; 106 if (bin > 0.0 && magnitude > peaks[0].db) { // && magnitude < 0) { 107 memmove (peaks + 1, peaks, sizeof (aubio_fpeak_t) * (MAX_PEAKS - 1)); 108 peaks[0].bin = bin; 109 peaks[0].db = magnitude; 110 } 106 111 } 107 }108 112 109 k = 0;110 for (l=1; l<MAX_PEAKS && peaks[l].bin > 0.0; l++) {111 sint_t harmonic;112 for (harmonic=5; harmonic>1; harmonic--) {113 if (peaks[0].bin / peaks[l].bin < harmonic+.02 &&114 peaks[0].bin / peaks[l].bin > harmonic-.02) {115 if (harmonic > (sint_t)maxharm &&116 peaks[0].db < peaks[l].db/2) {117 maxharm = harmonic;118 k = l;113 k = 0; 114 for (l = 1; l < MAX_PEAKS && peaks[l].bin > 0.0; l++) { 115 sint_t harmonic; 116 for (harmonic = 5; harmonic > 1; harmonic--) { 117 if (peaks[0].bin / peaks[l].bin < harmonic + .02 && 118 peaks[0].bin / peaks[l].bin > harmonic - .02) { 119 if (harmonic > (sint_t) maxharm && peaks[0].db < peaks[l].db / 2) { 120 maxharm = harmonic; 121 k = l; 122 } 119 123 } 120 124 } 121 125 } 122 }123 output->data[i][0] = peaks[k].bin;124 /* quick hack to clean output a bit */125 if (peaks[k].bin > 5000.)output->data[i][0] = 0.;126 output->data[i][0] = peaks[k].bin; 127 /* quick hack to clean output a bit */ 128 if (peaks[k].bin > 5000.) 129 output->data[i][0] = 0.; 126 130 } 127 131 } 128 132 129 void del_aubio_pitchfcomb (aubio_pitchfcomb_t * p) 133 void 134 del_aubio_pitchfcomb (aubio_pitchfcomb_t * p) 130 135 { 131 del_cvec (p->fftOut);132 del_fvec (p->fftLastPhase);133 del_fvec (p->win);134 del_fvec (p->winput);135 del_aubio_fft (p->fft);136 AUBIO_FREE (p);136 del_cvec (p->fftOut); 137 del_fvec (p->fftLastPhase); 138 del_fvec (p->win); 139 del_fvec (p->winput); 140 del_aubio_fft (p->fft); 141 AUBIO_FREE (p); 137 142 } 138
Note: See TracChangeset
for help on using the changeset viewer.