Skip to content

Commit

Permalink
Admit FFT window sizes that are not powers of two
Browse files Browse the repository at this point in the history
  • Loading branch information
anlthms committed Jul 1, 2016
1 parent bd6aade commit fbde8ef
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 30 deletions.
20 changes: 4 additions & 16 deletions loader/src/specgram.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ class Specgram {
_numCepstra(params->_numCepstra),
_window(0), _rng(id) {
assert(_stride != 0);
if (powerOfTwo(_windowSize) == false) {
throw std::runtime_error("Window size must be a power of 2");
}

_maxSignalSize = params->_clipDuration * params->_samplingFreq / 1000;
_buf = new char[_timeSteps * _windowSize * MAX_SAMPLE_SIZE];
if (params->_window != 0) {
Expand Down Expand Up @@ -139,33 +135,26 @@ class Specgram {
}
}

bool powerOfTwo(int num) {
while (((num % 2) == 0) && (num > 1)) {
num /= 2;
}
return (num == 1);
}

void none(int) {
}

void hann(int steps) {
for (int i = 0; i <= steps; i++) {
_window->at<float>(0, i) = 0.5 - 0.5 * cos((2.0 * PI * i) / steps);
_window->at<float>(0, i) = 0.5 - 0.5 * cos((2.0 * CV_PI * i) / steps);
}
}

void blackman(int steps) {
for (int i = 0; i <= steps; i++) {
_window->at<float>(0, i) = 0.42 -
0.5 * cos((2.0 * PI * i) / steps) +
0.08 * cos(4.0 * PI * i / steps);
0.5 * cos((2.0 * CV_PI * i) / steps) +
0.08 * cos(4.0 * CV_PI * i / steps);
}
}

void hamming(int steps) {
for (int i = 0; i <= steps; i++) {
_window->at<float>(0, i) = 0.54 - 0.46 * cos((2.0 * PI * i) / steps);
_window->at<float>(0, i) = 0.54 - 0.46 * cos((2.0 * CV_PI * i) / steps);
}
}

Expand Down Expand Up @@ -307,6 +296,5 @@ class Specgram {
Mat* _window;
Mat _fbank;
cv::RNG _rng;
constexpr static double PI = 3.14159265358979323846;
constexpr static int MAX_SAMPLE_SIZE = 4;
};
15 changes: 1 addition & 14 deletions neon/data/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

from __future__ import division
from future.utils import iteritems
import math
import logging
import numpy as np
import ctypes as ct
Expand Down Expand Up @@ -332,19 +331,7 @@ def set_shape(self):
self.feature = self._features_[self.feature_type]
self.window = self._windows_[self.window_type]
self.channel_count = 1
samples_per_frame = self.sampling_freq * self.frame_duration // 1000
# Get the closest power of 2.
log = int(math.log(samples_per_frame) / math.log(2))
min_pow = 2 ** log
max_pow = 2 ** (log + 1)

if (max_pow - samples_per_frame) < (samples_per_frame - min_pow):
self.window_size = max_pow
else:
self.window_size = min_pow

real_frame_duration = 1000 * self.window_size // self.sampling_freq
logger.info('Effective frame duration: %dms', real_frame_duration)
self.window_size = self.sampling_freq * self.frame_duration // 1000
assert self.overlap_percent < 100
self.overlap = self.window_size * self.overlap_percent // 100
self.stride = self.window_size - self.overlap
Expand Down

0 comments on commit fbde8ef

Please sign in to comment.