-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from lermert/master
numpy version for stretching
- Loading branch information
Showing
2 changed files
with
153 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
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,66 @@ | ||
import numpy as np | ||
import sys | ||
import os | ||
sys.path.append(os.getcwd()) | ||
from noise_module import stretching_vect, stretching | ||
from obspy.signal.invsim import cosine_taper | ||
import pytest | ||
import time | ||
|
||
# This short script is intended as a test for the stretching routine | ||
# it takes a generic sine curve with known stretching factor and ensures | ||
# that the stretching routines in noise_module always recover this factor | ||
# Note: The script has to be called from src/ directory, like | ||
# (in directory noisepy/src:) | ||
# python ../test/test_routines/test_stretching.py | ||
|
||
def test_stretching(): | ||
t = np.linspace(0., 9.95, 2500) # 0.5 % perturbation | ||
original_signal = np.sin(t * 10.) * cosine_taper(2500, p=0.75) | ||
|
||
t_stretch = np.linspace(0., 10.0, 2500) | ||
stretched_signal = np.interp(t, t_stretch, original_signal) | ||
|
||
|
||
para = {} | ||
para["dt"] = 1. / 250. | ||
para["twin"] = [0., 10.] | ||
para["freq"] = [9.9, 10.1] | ||
|
||
dvv, error, cc, cdp = stretching(ref=original_signal, cur=stretched_signal, | ||
dv_range=0.05, nbtrial=100, para=para) | ||
|
||
assert pytest.approx(cc) == 1.0 | ||
assert dvv + 0.5 < para["dt"] # assert result is -0.5% | ||
|
||
def test_stretching_vect(): | ||
t = np.linspace(0., 9.95, 2500) # 0.5 % perturbation | ||
original_signal = np.sin(t * 10.) * cosine_taper(2500, p=0.75) | ||
|
||
t_stretch = np.linspace(0., 10.0, 2500) | ||
stretched_signal = np.interp(t, t_stretch, original_signal) | ||
|
||
|
||
para = {} | ||
para["dt"] = 1. / 250. | ||
para["twin"] = [0., 10.] | ||
para["freq"] = [9.9, 10.1] | ||
|
||
dvv, error, cc, cdp = stretching_vect(ref=original_signal, cur=stretched_signal, | ||
dv_range=0.05, nbtrial=100, para=para) | ||
|
||
assert pytest.approx(cc) == 1.0 | ||
assert dvv + 0.5 < para["dt"] # assert result is -0.5% | ||
|
||
if __name__ == "__main__": | ||
print("Running stretching...") | ||
t = time.time() | ||
for i in range(300): | ||
test_stretching() | ||
print("Done stretching, no errors, %4.2fs." %(time.time()-t)) | ||
|
||
print("Running stretching using numpy...") | ||
t = time.time() | ||
for i in range(300): | ||
test_stretching_vect() | ||
print("Done stretching, no errors, %4.2fs." %(time.time()-t)) |