Exemplo n.º 1
0
 TemporalWindowFactory::TemporalWindowWrapper::TemporalWindowWrapper(unsigned int frameSize) {
   WindowFunction win;
   temporalWindow.resize(frameSize);
   std::vector<double>::iterator twIt = temporalWindow.begin();
   for (unsigned int i = 0; i < frameSize; i++) {
     *twIt = win.window(WINDOW_BLACKMAN, i, frameSize);
     std::advance(twIt, 1);
   }
 }
Exemplo n.º 2
0
 SpectrumAnalyser::SpectrumAnalyser(
   unsigned int f,
   const Parameters& params,
   ChromaTransformFactory& ctFactory
 ) {
   octaves = params.getOctaves();
   bandsPerSemitone = params.getBandsPerSemitone();
   hopSize = params.getHopSize();
   ct = ctFactory.getChromaTransform(f, params);
   unsigned int fftFrameSize = params.getFftFrameSize();
   fft = new FftAdapter(fftFrameSize);
   WindowFunction win;
   temporalWindow = std::vector<float>(fftFrameSize);
   for (unsigned int i = 0; i < fftFrameSize; i++) {
     temporalWindow[i] = win.window(params.getTemporalWindow(), i, fftFrameSize);
   }
 }
Exemplo n.º 3
0
  LowPassFilterPrivate::LowPassFilterPrivate(unsigned int inOrder, unsigned int frameRate, double cornerFrequency, unsigned int fftFrameSize) {
    if (inOrder % 2 != 0) {
      throw Exception("LPF order must be an even number");
    }
    if (inOrder > fftFrameSize / 4) {
      throw Exception("LPF order must be <= FFT frame size / 4");
    }
    order = inOrder;
    delay = order / 2;
    impulseLength = order + 1;
    double cutoffPoint = cornerFrequency / frameRate;
    InverseFftAdapter* ifft = new InverseFftAdapter(fftFrameSize);

    // Build frequency domain response
    double tau = 0.5 / cutoffPoint;
    for (unsigned int i = 0; i < fftFrameSize/2; i++) {
      double input = 0.0;
      if (i / (double) fftFrameSize <= cutoffPoint) {
        input = tau;
      }
      ifft->setInput(i, input, 0.0);
      ifft->setInput(fftFrameSize - i - 1, input, 0.0);
    }

    // inverse FFT to determine time-domain response
    ifft->execute();

    // TODO determine whether to handle bad_alloc
    coefficients.resize(impulseLength, 0.0);
    unsigned int centre = order / 2;
    gain = 0.0;
    WindowFunction win;

    for (unsigned int i = 0; i < impulseLength; i++) {
      // Grabbing the very end and the very beginning of the real FFT output?
      unsigned int index = (fftFrameSize - centre + i) % fftFrameSize;
      double coeff = ifft->getOutput(index);
      coeff *= win.window(WINDOW_HAMMING, i, impulseLength);
      coefficients[i] = coeff;
      gain += coeff;
    }

    delete ifft;
  }
Exemplo n.º 4
0
  LowPassFilter::LowPassFilter(unsigned int ord, unsigned int frameRate, float cornerFrequency, unsigned int fftFrameSize){
    // TODO: validate order is even
    order = ord;
    delay = order / 2;
    impulseLength = order + 1;
    float cutoffPoint = cornerFrequency / frameRate;
    FftAdapter* fft = new FftAdapter(fftFrameSize);

    // Build frequency domain response
    float tau = 0.5 / cutoffPoint;
    for (unsigned int i = 0; i < fftFrameSize/2; i++){
      float input = 0.0;
      if (i / (float) fftFrameSize <= cutoffPoint){
        input = tau;
      }
      fft->setInput(i, input);
      fft->setInput(fftFrameSize-i-1, input);
    }

    // inverse FFT to determine time-domain response
    fft->execute();

    // TODO determine whether to handle bad_alloc
    coefficients.resize(impulseLength, 0.0);
    unsigned int centre = order / 2;
    gain = 0.0;
    WindowFunction win;

    for (unsigned int i = 0; i < impulseLength; i++){
      // Grabbing the very end and the very beginning of the real FFT output?
      unsigned int index = (fftFrameSize - centre + i) % fftFrameSize;
      float coeff = fft->getOutputReal(index) / (float) fftFrameSize;
      coeff *= win.window(WINDOW_BLACKMAN, i, impulseLength);
      coefficients[i] = coeff;
      gain += coeff;
    }

    delete fft;
  }
Exemplo n.º 5
0
 std::vector<float> Segmentation::cosineRateOfChange(
   const Chromagram* const ch,
   unsigned int gaussianSize,
   float gaussianSigma
 ) const {
   unsigned int hops = ch->getHops();
   unsigned int bands = ch->getBands();
   // initialise to 1.0 (implies vectors are exactly the same)
   std::vector<float> cosineChanges(hops, 1.0);
   // determine cosine similarity
   for (unsigned int hop = 0; hop < hops; hop++) {
     // for each hop except the first
     if (hop > 0) {
       float top = 0.0;
       float bottomLeft = 0.0;
       float bottomRight = 0.0;
       for (unsigned int band = 0; band < bands; band++) {
         // add a tiny amount to each magnitude to guard against div by zero
         float magA = ch->getMagnitude(hop - 1, band) + 0.001;
         float magB = ch->getMagnitude(hop, band) + 0.001;
         top += magA * magB;
         bottomLeft  += magA * magA;
         bottomRight += magB * magB;
       }
       cosineChanges[hop] = top / (sqrt(bottomLeft) * sqrt(bottomRight));
     }
     // invert similarity so it represents change instead
     cosineChanges[hop] = 1.0 - cosineChanges[hop];
   }
   // build gaussian kernel for smoothing
   WindowFunction win;
   std::vector<float> gaussian(gaussianSize);
   for (unsigned int i = 0; i < gaussianSize; i++) {
     gaussian[i] = win.gaussianWindow(i, gaussianSize, gaussianSigma);
   }
   // smooth change vector
   return win.convolve(cosineChanges, gaussian);
 }
Exemplo n.º 6
0
void FeatureExtractionSpectral::calculate_magSpec(){
	int hopSize = this->get_hopSize();
	int winSize = this->get_winSize();
	int nRows = this->get_nRows(); // vertical time axis

	// memory allocation for m_magSpec;
	m_magSpec = (SAMPLE**)malloc(winSize*sizeof(SAMPLE*));
	for (int i = 0; i < winSize / 2 + 1; ++i){
		m_magSpec[i] = (SAMPLE*)malloc(nRows*sizeof(SAMPLE*));
	}

	// get buffer
	SAMPLE* signal = this->get_signal();

	// get window
	WindowFunction* windowObj = new WindowFunction(winSize);
	SAMPLE* window = windowObj->get_window();
	//SAMPLE* winSig = (SAMPLE*)malloc(winSize*sizeof(SAMPLE)); 
	std::vector<std::complex<SAMPLE>>winSig; // windowed sig

	// get the magnitude spectrum

	for (int i = 0; i < nRows; ++i){

		// hopping (happens in buffer) and windowing
		for (int j = 0; j < winSize; ++j){
			winSig[j] = signal[i*hopSize + j] * window[j];
		}
		std::vector<std::complex<SAMPLE>> fftOut;
		fftOut = FFT::fft(winSig);

		// put the fftout to m_magSpec
		for (int j = 0; j < winSize / 2 + 1; ++j){
			m_magSpec[i][j] = (SAMPLE)abs(fftOut[i]);
		}
	}
}