annotate NoveltyCurveProcessor.cpp @ 37:44d8e5dc1902

Split out Makefile to separate single-platform versions and an .inc file
author Chris Cannam
date Fri, 12 Sep 2014 14:54:55 +0100
parents a824d7d14eb9
children 89af6709f562
rev   line source
c@5 1 //
c@17 2 // NoveltyCurveProcessor.cpp
c@5 3 // Tempogram
c@5 4 //
c@5 5 // Created by Carl Bussey on 10/07/2014.
c@5 6 // Copyright (c) 2014 Carl Bussey. All rights reserved.
c@5 7 //
c@5 8
c@11 9 //Spectrogram dimensions should be flipped?
c@11 10
c@14 11 #include "NoveltyCurveProcessor.h"
c@5 12 using namespace std;
c@5 13
c@22 14 NoveltyCurveProcessor::NoveltyCurveProcessor(const float &samplingFrequency, const size_t &fftLength, const size_t &compressionConstant) :
c@5 15 m_samplingFrequency(samplingFrequency),
c@7 16 m_fftLength(fftLength),
c@7 17 m_blockSize(fftLength/2 + 1),
c@5 18 m_compressionConstant(compressionConstant),
c@5 19 m_numberOfBands(5),
c@13 20 m_pBandBoundaries(0),
c@13 21 m_pBandSum(0)
c@5 22 {
c@5 23 initialise();
c@5 24 }
c@5 25
c@14 26 NoveltyCurveProcessor::~NoveltyCurveProcessor(){
c@5 27 cleanup();
c@5 28 }
c@5 29
c@9 30 //allocate all space and set variable
c@5 31 void
c@14 32 NoveltyCurveProcessor::initialise(){
c@5 33
c@13 34 // for bandwise processing, the band is split into 5 bands. m_pBandBoundaries contains the upper and lower bin boundaries for each band.
c@13 35 m_pBandBoundaries = new int[m_numberOfBands+1];
c@13 36 m_pBandBoundaries[0] = 0;
c@13 37 for (unsigned int band = 1; band < m_numberOfBands; band++){
c@13 38 float lowFreq = 500*pow(2.5, (int)band-1);
c@13 39 m_pBandBoundaries[band] = m_fftLength*lowFreq/m_samplingFrequency;
Chris@33 40 if (m_pBandBoundaries[band] > (int)m_blockSize) {
Chris@33 41 m_pBandBoundaries[band] = m_blockSize;
Chris@33 42 }
c@5 43 }
c@13 44 m_pBandBoundaries[m_numberOfBands] = m_blockSize;
c@13 45 m_pBandSum = new float [m_numberOfBands];
c@5 46 }
c@5 47
c@9 48 //delete space allocated in initialise()
c@5 49 void
c@14 50 NoveltyCurveProcessor::cleanup(){
c@13 51 delete []m_pBandBoundaries;
c@13 52 m_pBandBoundaries = 0;
c@13 53 delete []m_pBandSum;
c@13 54 m_pBandSum = 0;
c@5 55 }
c@5 56
c@9 57 //subtract local average of novelty curve
c@9 58 //uses m_hannWindow as filter
c@14 59 void NoveltyCurveProcessor::subtractLocalAverage(vector<float> &noveltyCurve, const size_t &smoothLength) const
c@13 60 {
c@22 61 int numberOfBlocks = noveltyCurve.size();
c@22 62 vector<float> localAverage(numberOfBlocks);
c@5 63
c@13 64 float * m_hannWindow = new float[smoothLength];
c@13 65 WindowFunction::hanning(m_hannWindow, smoothLength, true);
c@9 66
c@22 67 FIRFilter filter(numberOfBlocks, smoothLength);
c@15 68 filter.process(&noveltyCurve[0], m_hannWindow, &localAverage[0], FIRFilter::middle);
c@5 69
c@22 70 for (int i = 0; i < numberOfBlocks; i++){
c@5 71 noveltyCurve[i] -= localAverage[i];
c@5 72 noveltyCurve[i] = noveltyCurve[i] >= 0 ? noveltyCurve[i] : 0;
c@5 73 }
c@9 74
c@11 75 delete []m_hannWindow;
c@13 76 m_hannWindow = 0;
c@5 77 }
c@5 78
c@9 79 //smoothed differentiator filter. Flips upper half of hanning window about y-axis to create coefficients.
c@22 80 void NoveltyCurveProcessor::smoothedDifferentiator(SpectrogramTransposed &spectrogramTransposed, const size_t &smoothLength) const
c@13 81 {
c@22 82 int numberOfBlocks = spectrogramTransposed[0].size();
c@22 83
c@7 84 float * diffHannWindow = new float [smoothLength];
c@7 85 WindowFunction::hanning(diffHannWindow, smoothLength, true);
c@7 86
c@7 87 if(smoothLength%2) diffHannWindow[(smoothLength+1)/2 - 1] = 0;
c@20 88 for(int i = (smoothLength+1)/2; i < (int)smoothLength; i++){
c@7 89 diffHannWindow[i] = -diffHannWindow[i];
c@7 90 }
c@7 91
c@22 92 FIRFilter smoothFilter(numberOfBlocks, smoothLength);
c@7 93
c@20 94 for (int i = 0; i < (int)m_blockSize; i++){
c@22 95 smoothFilter.process(&spectrogramTransposed[i][0], diffHannWindow, &spectrogramTransposed[i][0], FIRFilter::middle);
c@7 96 }
c@7 97 }
c@7 98
c@9 99 //half rectification (set negative to zero)
c@24 100 void NoveltyCurveProcessor::halfWaveRectify(Spectrogram &spectrogram) const
c@13 101 {
c@24 102 int length = spectrogram.size();
c@25 103 int height = length > 0 ? spectrogram[0].size() : 0;
c@22 104
c@24 105 for (int i = 0; i < length; i++){
c@24 106 for (int j = 0; j < height; j++){
c@24 107 if (spectrogram[i][j] < 0.0) spectrogram[i][j] = 0.0;
c@7 108 }
c@7 109 }
c@7 110 }
c@7 111
c@9 112 //process method
c@5 113 vector<float>
c@22 114 NoveltyCurveProcessor::spectrogramToNoveltyCurve(const Spectrogram &spectrogram) const //make argument const &
c@13 115 {
c@22 116 int numberOfBlocks = spectrogram.size();
c@22 117 std::vector<float> noveltyCurve(numberOfBlocks);
c@25 118 SpectrogramTransposed spectrogramTransposed(m_blockSize, vector<float>(spectrogram.size()));
c@5 119
c@9 120 //normalise and log spectrogram
c@25 121 float normaliseScale = SpectrogramProcessor::calculateMax(spectrogram);
c@22 122 for (int block = 0; block < (int)numberOfBlocks; block++){
c@20 123 for (int k = 0; k < (int)m_blockSize; k++){
c@25 124 float magnitude = spectrogram[block][k];
c@25 125 if(normaliseScale != 0.0) magnitude /= normaliseScale; //normalise
c@25 126 spectrogramTransposed[k][block] = log(1+m_compressionConstant*magnitude);
c@7 127 }
c@7 128 }
c@24 129
c@9 130 //smooted differentiator
c@22 131 smoothedDifferentiator(spectrogramTransposed, 5); //make smoothLength a parameter!
c@9 132 //halfwave rectification
c@22 133 halfWaveRectify(spectrogramTransposed);
c@7 134
c@9 135 //bandwise processing
c@22 136 for (int block = 0; block < (int)numberOfBlocks; block++){
c@20 137 for (int band = 0; band < (int)m_numberOfBands; band++){
c@13 138 int k = m_pBandBoundaries[band];
c@13 139 int bandEnd = m_pBandBoundaries[band+1];
c@13 140 m_pBandSum[band] = 0;
c@5 141
c@7 142 while(k < bandEnd){
c@22 143 m_pBandSum[band] += spectrogramTransposed[k][block];
c@7 144 k++;
c@5 145 }
c@5 146 }
c@5 147 float total = 0;
c@20 148 for(int band = 0; band < (int)m_numberOfBands; band++){
c@13 149 total += m_pBandSum[band];
c@5 150 }
c@13 151 noveltyCurve[block] = total/m_numberOfBands;
c@5 152 }
c@5 153
c@9 154 //subtract local averages
c@29 155 subtractLocalAverage(noveltyCurve, 65); //maybe smaller?
c@5 156
c@13 157 return noveltyCurve;
c@7 158 }