Skip to content

Commit

Permalink
Fixes some types and updates tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Siegfried Gündert committed Feb 16, 2023
1 parent 3758b5b commit 7f8aefa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
6 changes: 3 additions & 3 deletions syncsweptsine.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ def _calculate_inverted_sweep_spectrum(self):
sweepperiod = self._sweepperiod
startfreq = self._startfreq
freq = _np.linspace(0, samplerate/2, int(_np.round(self.fftlen/2+1)))
spectrum = _np.zeros_like(freq, dtype=_np.complex)
spectrum = _np.zeros_like(freq, dtype=_np.complex_)
# eq. 43 definition of the inverse spectrum in frequency domain
spectrum[1:] = (
2*_np.sqrt(freq[1:]/sweepperiod)
Expand Down Expand Up @@ -783,7 +783,7 @@ def hir_index(self, order, length, delay=0):
Delay of system under test the hhir was derived from.
"""
return _np.arange(length)+delay+self.hir_sample_position(order)
return _np.arange(length, dtype=int)+int(delay)+self.hir_sample_position(order)

def max_hir_length(self, order):
"""Returns the maximum length of mpulse responses for given orders.
Expand Down Expand Up @@ -856,7 +856,7 @@ def from_sweeps(cls, syncsweep, measuredsweep, fftlen=None, regularize=1e-6):
Length of the calculated ffts. fftlen will be guessed from measuredsweep length if fftlen is None.
"""
fftlen = fftlen or _np.int(2**_np.ceil(1+_np.log2(len(measuredsweep))))
fftlen = fftlen or int(2**_np.ceil(1+_np.log2(len(measuredsweep))))
rspec = _np.fft.rfft(measuredsweep, fftlen)
rinvspec = InvertedSyncSweepSpectrum.from_sweep(syncsweep, fftlen=fftlen).spectrum
freq = _np.fft.rfftfreq(fftlen, 1/syncsweep.samplerate)
Expand Down
11 changes: 6 additions & 5 deletions test_syncsweptsine.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ def test_higher_harmonic_impulse_response():
sweep = SyncSweep(10, 10000, 5, 20000)
hhir = HigherHarmonicImpulseResponse.from_sweeps(sweep, sweep)
assume(np.all(np.array(hhir) == hhir.hhir))
hir = hhir.harmonic_impulse_response(order=1, length=1024, delay=0, window=True)
hir = hhir.harmonic_impulse_response(order=1, length=1024, delay=-512, window=True)
assume(type(hir) == np.ndarray)
assume(len(hir) == 1024)
assume(np.argmax(hir)==512)
hir = hhir.harmonic_impulse_response(order=1, length=1024, delay=0, window=np.linspace(0, 1, 1024))
hir = hhir.harmonic_impulse_response(order=1, length=1024, delay=-512, window=np.linspace(0, 1, 1024))
assume(type(hir) == np.ndarray)
assume(len(hir) == 1024)
assume(np.argmax(hir)==512)
Expand Down Expand Up @@ -238,18 +238,19 @@ def nonlinear_system(sig):
outsweep = nonlinear_system(np.array(sweep))
hhir = HigherHarmonicImpulseResponse.from_sweeps(sweep, outsweep, regularize=False)
hm = HammersteinModel.from_higher_harmonic_impulse_response(
hhir, 2048, orders=(1, 2, 3), delay=0)
hhir, 2048, orders=(1, 2, 3), delay=-1024) # -delay since irs are acausal in this case.
assume(hm.orders==(1, 2, 3))
x = np.array([1]+127*[0])
hm.filter(x)
assume(hm.__repr__().startswith('HammersteinModel('))
expectedcoeffs = [1.0, 0.25, 0.125]
for kernel, expc in zip(hm.kernels, expectedcoeffs):
print(abs(np.median(abs(kernel.frf[100:200]))), expc)
assume(abs(np.median(abs(kernel.frf[100:200]))-expc) < 0.01)

with pytest.raises(ValueError):
hm = HammersteinModel.from_higher_harmonic_impulse_response(
hhir, 96e4, orders=(1, 2, 3), delay=0)
hhir, hhir.max_hir_length(1)+1, orders=(1, 2, 3), delay=-1024)
lm = LinearModel.from_hammerstein_model(hm)


Expand All @@ -266,7 +267,7 @@ def nonlinear_system(sig):
y = lm.filter(x)
with pytest.raises(ValueError):
hm = LinearModel.from_higher_harmonic_impulse_response(
hhir, 96e4, delay=0)
hhir, hhir.max_hir_length(1)+1, delay=0)


if __name__ == "__main__":
Expand Down

0 comments on commit 7f8aefa

Please sign in to comment.