Skip to content

Commit 8beff00

Browse files
committed
avoid unnecessary computing with pickling computed enf-samples
1 parent 2ca8b05 commit 8beff00

2 files changed

Lines changed: 43 additions & 6 deletions

File tree

lib/pickling.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import bz2
2+
import pickle
3+
import _pickle as cPickle
4+
import sys
5+
6+
7+
# Pickle a file and then compress it into a file with extension
8+
def compress_pickle(filename, data):
9+
sys.stderr.write("loading pickle %s..." % filename)
10+
with bz2.BZ2File(filename, 'w') as f:
11+
cPickle.dump(data, f)
12+
13+
# Load any compressed pickle file
14+
def decompress_pickle(filename):
15+
sys.stderr.write("saving pickle %s..." % filename)
16+
data = bz2.BZ2File(filename, 'rb')
17+
data = cPickle.load(data)
18+
return data
19+

main.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
from scipy import signal
1111
from tqdm import tqdm
1212

13+
from lib.pickling import *
14+
from os.path import exists
15+
1316
def load_wav(fpath):
1417
"""Loads a .wav file and returns the data and sample rate.
1518
@@ -214,21 +217,36 @@ def plot_series_ax(ax, series, label=None):
214217
ax.plot(t, series, label=label)
215218

216219

220+
def wav_to_enf(filename, nominal_freq, freq_band_size, harmonic_n=1):
221+
"""
222+
This code attempts to load a pickle file that contains the enf samples.
223+
If the pickle file does not exist, the code loads the corresponding wav file,
224+
processes it, and saves the enf samples in a new pickle file.
225+
This approach prevents unnecessary loading and computing.
226+
"""
227+
pklfilename = "." + filename + ".pkl"
228+
if exists(pklfilename):
229+
return decompress_pickle(pklfilename)
230+
else:
231+
ref_data, ref_fs = load_wav(filename)
232+
enf = enf_series(ref_data, ref_fs, nominal_freq, freq_band_size, harmonic_n)
233+
compress_pickle(pklfilename, enf)
234+
return enf
235+
217236
if __name__ == "__main__":
218237
nominal_freq = 50
219238
freq_band_size = 0.2
220239

221-
# !!!: make sure to run ./bin/download-example-files first
222-
ref_data, ref_fs = load_wav("./001_ref.wav")
240+
refwav = './001_ref.wav'
241+
wav = "./001.wav"
223242

224-
ref_enf_output = enf_series(ref_data, ref_fs, nominal_freq, freq_band_size)
243+
# !!!: make sure to run ./bin/download-example-files first
244+
ref_enf_output = wav_to_enf(refwav, nominal_freq, freq_band_size, harmonic_n=1)
225245
ref_enf = ref_enf_output['enf']
226246

227247
# !!!: make sure to run ./bin/download-example-files first
228-
data, fs = load_wav("./001.wav")
229-
230248
harmonic_n = 1
231-
enf_output = enf_series(data, fs, nominal_freq, freq_band_size, harmonic_n=2)
249+
enf_output = wav_to_enf(refwav, nominal_freq, freq_band_size, harmonic_n=2)
232250
target_enf = enf_output['enf']
233251

234252
stft = enf_output['stft']

0 commit comments

Comments
 (0)