annotate NoveltyCurveProcessor.cpp @ 31:f795b84c1ff2

This include change seems necessary for my compiler
author Chris Cannam
date Thu, 04 Sep 2014 10:08:09 +0100
parents 1ad47a9afc2e
children a824d7d14eb9
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;
c@5 40 }
c@13 41 m_pBandBoundaries[m_numberOfBands] = m_blockSize;
c@5 42
c@13 43 m_pBandSum = new float [m_numberOfBands];
c@5 44 }
c@5 45
c@9 46 //delete space allocated in initialise()
c@5 47 void
c@14 48 NoveltyCurveProcessor::cleanup(){
c@13 49 delete []m_pBandBoundaries;
c@13 50 m_pBandBoundaries = 0;
c@13 51 delete []m_pBandSum;
c@13 52 m_pBandSum = 0;
c@5 53 }
c@5 54
c@9 55 //subtract local average of novelty curve
c@9 56 //uses m_hannWindow as filter
c@14 57 void NoveltyCurveProcessor::subtractLocalAverage(vector<float> &noveltyCurve, const size_t &smoothLength) const
c@13 58 {
c@22 59 int numberOfBlocks = noveltyCurve.size();
c@22 60 vector<float> localAverage(numberOfBlocks);
c@5 61
c@13 62 float * m_hannWindow = new float[smoothLength];
c@13 63 WindowFunction::hanning(m_hannWindow, smoothLength, true);
c@9 64
c@22 65 FIRFilter filter(numberOfBlocks, smoothLength);
c@15 66 filter.process(&noveltyCurve[0], m_hannWindow, &localAverage[0], FIRFilter::middle);
c@5 67
c@22 68 for (int i = 0; i < numberOfBlocks; i++){
c@5 69 noveltyCurve[i] -= localAverage[i];
c@5 70 noveltyCurve[i] = noveltyCurve[i] >= 0 ? noveltyCurve[i] : 0;
c@5 71 }
c@9 72
c@11 73 delete []m_hannWindow;
c@13 74 m_hannWindow = 0;
c@5 75 }
c@5 76
c@9 77 //smoothed differentiator filter. Flips upper half of hanning window about y-axis to create coefficients.
c@22 78 void NoveltyCurveProcessor::smoothedDifferentiator(SpectrogramTransposed &spectrogramTransposed, const size_t &smoothLength) const
c@13 79 {
c@22 80 int numberOfBlocks = spectrogramTransposed[0].size();
c@22 81
c@7 82 float * diffHannWindow = new float [smoothLength];
c@7 83 WindowFunction::hanning(diffHannWindow, smoothLength, true);
c@7 84
c@7 85 if(smoothLength%2) diffHannWindow[(smoothLength+1)/2 - 1] = 0;
c@20 86 for(int i = (smoothLength+1)/2; i < (int)smoothLength; i++){
c@7 87 diffHannWindow[i] = -diffHannWindow[i];
c@7 88 }
c@7 89
c@22 90 FIRFilter smoothFilter(numberOfBlocks, smoothLength);
c@7 91
c@20 92 for (int i = 0; i < (int)m_blockSize; i++){
c@22 93 smoothFilter.process(&spectrogramTransposed[i][0], diffHannWindow, &spectrogramTransposed[i][0], FIRFilter::middle);
c@7 94 }
c@7 95 }
c@7 96
c@9 97 //half rectification (set negative to zero)
c@24 98 void NoveltyCurveProcessor::halfWaveRectify(Spectrogram &spectrogram) const
c@13 99 {
c@24 100 int length = spectrogram.size();
c@25 101 int height = length > 0 ? spectrogram[0].size() : 0;
c@22 102
c@24 103 for (int i = 0; i < length; i++){
c@24 104 for (int j = 0; j < height; j++){
c@24 105 if (spectrogram[i][j] < 0.0) spectrogram[i][j] = 0.0;
c@7 106 }
c@7 107 }
c@7 108 }
c@7 109
c@9 110 //process method
c@5 111 vector<float>
c@22 112 NoveltyCurveProcessor::spectrogramToNoveltyCurve(const Spectrogram &spectrogram) const //make argument const &
c@13 113 {
c@22 114 int numberOfBlocks = spectrogram.size();
c@22 115 std::vector<float> noveltyCurve(numberOfBlocks);
c@25 116 SpectrogramTransposed spectrogramTransposed(m_blockSize, vector<float>(spectrogram.size()));
c@5 117
c@9 118 //normalise and log spectrogram
c@25 119 float normaliseScale = SpectrogramProcessor::calculateMax(spectrogram);
c@22 120 for (int block = 0; block < (int)numberOfBlocks; block++){
c@20 121 for (int k = 0; k < (int)m_blockSize; k++){
c@25 122 float magnitude = spectrogram[block][k];
c@25 123 if(normaliseScale != 0.0) magnitude /= normaliseScale; //normalise
c@25 124 spectrogramTransposed[k][block] = log(1+m_compressionConstant*magnitude);
c@7 125 }
c@7 126 }
c@24 127
c@9 128 //smooted differentiator
c@22 129 smoothedDifferentiator(spectrogramTransposed, 5); //make smoothLength a parameter!
c@9 130 //halfwave rectification
c@22 131 halfWaveRectify(spectrogramTransposed);
c@7 132
c@9 133 //bandwise processing
c@22 134 for (int block = 0; block < (int)numberOfBlocks; block++){
c@20 135 for (int band = 0; band < (int)m_numberOfBands; band++){
c@13 136 int k = m_pBandBoundaries[band];
c@13 137 int bandEnd = m_pBandBoundaries[band+1];
c@13 138 m_pBandSum[band] = 0;
c@5 139
c@7 140 while(k < bandEnd){
c@22 141 m_pBandSum[band] += spectrogramTransposed[k][block];
c@7 142 k++;
c@5 143 }
c@5 144 }
c@5 145 float total = 0;
c@20 146 for(int band = 0; band < (int)m_numberOfBands; band++){
c@13 147 total += m_pBandSum[band];
c@5 148 }
c@13 149 noveltyCurve[block] = total/m_numberOfBands;
c@5 150 }
c@5 151
c@9 152 //subtract local averages
c@29 153 subtractLocalAverage(noveltyCurve, 65); //maybe smaller?
c@5 154
c@13 155 return noveltyCurve;
c@7 156 }