def _model(self, slice_size, settings): STFT = stft(resample_to=SR11025(), store_fft=True) def pack(x): arr = np.zeros((len(x), 16), dtype=np.uint64) return ArrayWithUnits(arr, [x.dimensions[0], IdentityDimension()]) class Model(STFT, settings): binary = ArrayWithUnitsFeature( Binarize, predicate=lambda data: data >= 0, needs=STFT.fft, store=False) sliced = ArrayWithUnitsFeature( Slice, sl=slice(0, slice_size), needs=binary, store=True) packed = ArrayWithUnitsFeature( pack, needs=sliced, store=True) return Model
def setUp(self): self.samplerate = SR44100() self.wscheme = HalfLapped() self.STFT = stft( store_fft=True, resample_to=self.samplerate, wscheme=self.wscheme)
def test_square_form_no_overlap_add(self): samplerate = SR11025() BaseModel = stft(resample_to=samplerate) windowing_func = OggVorbisWindowingFunc() scale = GeometricScale(20, 5000, 0.1, 25) @simple_in_memory_settings class Document(BaseModel): long_windowed = ArrayWithUnitsFeature( SlidingWindow, wscheme=SampleRate(frequency=Milliseconds(500), duration=Seconds(1)), wfunc=windowing_func, needs=BaseModel.resampled, store=True) dct = ArrayWithUnitsFeature(DCT, scale_always_even=True, needs=long_windowed, store=True) mdct = FrequencyAdaptiveFeature(FrequencyAdaptiveTransform, transform=scipy.fftpack.idct, scale=scale, needs=dct, store=True) synth = TickSynthesizer(SR22050()) samples = synth.synthesize(Seconds(5), Milliseconds(200)) _id = Document.process(meta=samples.encode()) doc = Document(_id) square = doc.mdct.square(30) self.assertEqual(3, square.ndim) self.assertEqual(30, square.shape[1]) self.assertEqual(25, square.shape[2])
def can_invert_fft(self, samplerate): base_cls = stft(resample_to=samplerate, store_fft=True, store_windowed=True) @simple_in_memory_settings class Document(base_cls): pass synth = SineSynthesizer(samplerate) audio = synth.synthesize(Seconds(2), freqs_in_hz=[440., 880.]) _id = Document.process(meta=audio.encode()) doc = Document(_id) fft_synth = FFTSynthesizer() recon = fft_synth.synthesize(doc.fft) self.assertIsInstance(recon, ArrayWithUnits) self.assertEqual(audio.dimensions, recon.dimensions)
def test_can_encode_and_decode_variable_rate_time_Series(self): class TimestampEmitter(ff.Node): def __init__(self, needs=None): super(TimestampEmitter, self).__init__(needs=needs) self.pos = Picoseconds(0) def _process(self, data): td = data.dimensions[0] frequency = td.frequency timestamps = [self.pos + (i * frequency) for i, d in enumerate(data) if random() > 0.9] slices = TimeSlice.slices(timestamps) yield VariableRateTimeSeries( (ts, np.zeros(0)) for ts in slices) self.pos += frequency * len(data) graph = stft(store_fft=True) @simple_in_memory_settings class Document(graph): slices = TimeSliceFeature( TimestampEmitter, needs=graph.fft, store=True) pooled = VariableRateTimeSeriesFeature( Pooled, op=np.max, axis=0, needs=(slices, graph.fft), store=False) signal = NoiseSynthesizer(SR44100())\ .synthesize(Seconds(10))\ .encode() _id = Document.process(meta=signal) doc = Document(_id) self.assertIsInstance(doc.pooled, VariableRateTimeSeries) self.assertEqual(doc.fft.shape[1], doc.pooled.slicedata.shape[1])
def test_square_form_no_overlap_add(self): samplerate = SR11025() BaseModel = stft(resample_to=samplerate) windowing_func = OggVorbisWindowingFunc() scale = GeometricScale(20, 5000, 0.1, 25) @simple_in_memory_settings class Document(BaseModel): long_windowed = ArrayWithUnitsFeature( SlidingWindow, wscheme=SampleRate( frequency=Milliseconds(500), duration=Seconds(1)), wfunc=windowing_func, needs=BaseModel.resampled, store=True) dct = ArrayWithUnitsFeature( DCT, scale_always_even=True, needs=long_windowed, store=True) mdct = FrequencyAdaptiveFeature( FrequencyAdaptiveTransform, transform=scipy.fftpack.idct, scale=scale, needs=dct, store=True) synth = TickSynthesizer(SR22050()) samples = synth.synthesize(Seconds(5), Milliseconds(200)) _id = Document.process(meta=samples.encode()) doc = Document(_id) square = doc.mdct.square(30) self.assertEqual(3, square.ndim) self.assertEqual(30, square.shape[1]) self.assertEqual(25, square.shape[2])
def setUp(self): self.samplerate = SR44100() self.wscheme = HalfLapped() self.STFT = stft(store_fft=True, resample_to=self.samplerate, wscheme=self.wscheme)
class Document(stft(store_fft=True)): pass