-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1 Write a simple test for file I/O.
- Loading branch information
Showing
3 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
test: | ||
nosetests | ||
|
||
install: | ||
pip install tfr | ||
|
||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import numpy as np | ||
import os | ||
import tempfile | ||
|
||
# Note that nose prepends the project directory to the sys.path so that we | ||
# do not import from the system-wide package! | ||
|
||
from tfr import files | ||
|
||
|
||
def test_array_should_be_saved_to_a_file(): | ||
fs = 1000 | ||
x = sine_wave(5.0, fs) | ||
with tempfile.NamedTemporaryFile(delete=True) as tmpfile: | ||
file_name = tmpfile.name | ||
files.save_wav(x, file_name, fs=fs) | ||
assert os.path.exists(file_name) | ||
|
||
def test_array_should_be_loaded_from_a_file(): | ||
fs = 1000 | ||
x = sine_wave(5.0, fs) | ||
with tempfile.NamedTemporaryFile(delete=False) as tmpfile: | ||
file_name = tmpfile.name | ||
files.save_wav(x, file_name, fs=fs) | ||
x_loaded, fs_loaded = files.load_wav(file_name) | ||
|
||
assert fs_loaded == fs | ||
# 16-bit quantization error is OK | ||
assert np.allclose(x_loaded, x, atol=(1 / (((2**15)) - 1))) | ||
|
||
def sine_wave(duration, fs): | ||
return np.sin(np.linspace(0, duration, duration * fs)) |