Skip to content

Commit

Permalink
Switch to windowOverlap instead of fourierOverlap
Browse files Browse the repository at this point in the history
The wording is more clear and accurate with the new window length variable. This commit also
updates the simulator to better handle the new window length functionality.
  • Loading branch information
nathanntg committed Jan 5, 2016
1 parent 110c414 commit b0dd4a3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion SyllableDetector/SyllableDetector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class SyllableDetector: NSObject, AVCaptureAudioDataOutputSampleBufferDelegate
self.config = config

// initialize the FFT
shortTimeFourierTransform = CircularShortTimeFourierTransform(windowLength: config.windowLength, withOverlap: config.fourierOverlap, fftSizeOf: config.fourierLength)
shortTimeFourierTransform = CircularShortTimeFourierTransform(windowLength: config.windowLength, withOverlap: config.windowOverlap, fftSizeOf: config.fourierLength)
shortTimeFourierTransform.windowType = WindowType.Hamming

// store frequency indices
Expand Down
31 changes: 13 additions & 18 deletions SyllableDetector/SyllableDetectorConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ struct SyllableDetectorConfig
}

var samplingRate: Double // eqv: samplerate
var windowLength: Int
var fourierLength: Int // eqv: FFT_SIZE
var fourierOverlap: Int // eqv: NOVERLAP = FFT_SIZE - (floor(samplerate * FFT_TIME_SHIFT))
let fourierLength: Int // eqv: FFT_SIZE
let windowLength: Int
var windowOverlap: Int // eqv: NOVERLAP = FFT_SIZE - (floor(samplerate * FFT_TIME_SHIFT))

let freqRange: (Double, Double) // eqv: freq_range
let timeRange: Int // eqv: time_window_steps = double(floor(time_window / timestep))
Expand All @@ -46,28 +46,23 @@ struct SyllableDetectorConfig
mutating func modifySamplingRate(newSamplingRate: Double) {
// store old things
let oldSamplingRate = samplingRate
let oldFourierLength = fourierLength
let oldFourierOverlap = fourierOverlap
let oldWindowOverlap = windowOverlap

if oldSamplingRate == newSamplingRate { return }

// window offset (difference in time between two consecutive windows)
// calculated using old values, but should be constant even after the new values
let windowOffset = Double(windowLength - oldWindowOverlap) / oldSamplingRate

// get new approximate fourier length
let newApproximateFourierLength = newSamplingRate * Double(oldFourierLength) / oldSamplingRate

// convert to closest power of 2
let newFourierLength = 1 << Int(round(log2(newApproximateFourierLength)))

// get new fourier overlap
let newFourierOverlap = newFourierLength - Int(round(newSamplingRate * Double(oldFourierLength - oldFourierOverlap) / oldSamplingRate))
let newWindowOverlap = windowLength - Int(round(windowOffset * newSamplingRate))

// change the to new things
samplingRate = newSamplingRate
fourierLength = newFourierLength
fourierOverlap = newFourierOverlap
windowOverlap = newWindowOverlap

DLog("New fourier length: \(newFourierLength)")
DLog("New fourier overlap: \(newFourierOverlap)")
DLog("Window offset: \(windowOffset)")
DLog("Sanity check: \(Double(windowLength - oldWindowOverlap) / oldSamplingRate)")
DLog("Window overlap: OLD \(oldWindowOverlap) NEW \(newWindowOverlap)")
}
}

Expand Down Expand Up @@ -234,7 +229,7 @@ extension SyllableDetectorConfig
}

// fourier length: int
fourierOverlap = try SyllableDetectorConfig.parseInt("fourierOverlap", from: data)
windowOverlap = try SyllableDetectorConfig.parseInt("windowOverlap", from: data)

// frequency range: double, double
let potentialFreqRange = try SyllableDetectorConfig.parseDoubleArray("freqRange", withCount: 2, from: data)
Expand Down
9 changes: 7 additions & 2 deletions SyllableDetector/ViewControllerSimulator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ class ViewControllerSimulator: NSViewController {
assert(status == noErr)

// processing
var nextCount: Int = sd.config.fourierLength + ((sd.config.fourierLength - sd.config.fourierOverlap) * (sd.config.timeRange - 1)), nextValue: Float = 0.0
var nextCount: Int = sd.config.windowLength + ((sd.config.windowLength - sd.config.windowOverlap) * (sd.config.timeRange - 1)), nextValue: Float = 0.0
if sd.config.windowOverlap < 0 {
nextCount = nextCount - sd.config.windowOverlap // since gap is applied even to the first data set
}
var samplePosition: Int64 = 0
let gcdGroup = dispatch_group_create()
let gcdQueue = dispatch_queue_create("Encode", DISPATCH_QUEUE_SERIAL)
Expand Down Expand Up @@ -296,7 +299,7 @@ class ViewControllerSimulator: NSViewController {
}

// length
var l = sd.config.fourierLength - sd.config.fourierOverlap
var l = sd.config.windowLength - sd.config.windowOverlap

for ; 0 < l && i < numSamples; ++i, --l {
newSamples[i] = v
Expand Down Expand Up @@ -325,6 +328,8 @@ class ViewControllerSimulator: NSViewController {

// append sample buffer
if !avWriterInput.appendSampleBuffer(newSampleBuffer!) {
DLog("failed to write sample buffer \(avWriter.status) \(avWriter.error)")
avReader.cancelReading() // cancel reading
completedOrFailed = true
}
}
Expand Down
4 changes: 2 additions & 2 deletions convert_to_text.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ function convert_to_text(fn, mat)

fprintf(fh, '# AUTOMATICALLY GENERATED SYLLABLE DETECTOR CONFIGURATION\n');
fprintf(fh, 'samplingRate = %.1f\n', f.samplerate);
fprintf(fh, 'windowLength = %d\n', f.win_size);
fprintf(fh, 'fourierLength = %d\n', f.fft_size);
fprintf(fh, 'fourierOverlap = %d\n', f.fft_size - f.fft_time_shift);
fprintf(fh, 'windowLength = %d\n', f.win_size);
fprintf(fh, 'windowOverlap = %d\n', f.fft_size - f.fft_time_shift);

fprintf(fh, 'freqRange = %.1f, %.1f\n', f.freq_range(1), f.freq_range(end));
fprintf(fh, 'timeRange = %d\n', f.time_window_steps);
Expand Down

0 comments on commit b0dd4a3

Please sign in to comment.