Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #153 from FRidh/cepstrum
Browse files Browse the repository at this point in the history
ENH: remove unnecessary N, rename N to n for compatibility
  • Loading branch information
FRidh committed Jul 17, 2015
2 parents 132a7d6 + 01aff3e commit cb1ad7e
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions acoustics/cepstrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
__all__ = ['complex_cepstrum', 'real_cepstrum', 'inverse_complex_cepstrum',
'minimum_phase']

def complex_cepstrum(x, N=None):
def complex_cepstrum(x, n=None):
"""Compute the complex cepstrum of a real sequence.
Parameters
----------
x : ndarray
Real sequence to compute complex cepstrum of.
N : {None, int}, optional
n : {None, int}, optional
Length of the Fourier transform.
Returns
Expand Down Expand Up @@ -92,21 +92,20 @@ def _unwrap(phase):
unwrapped -= np.pi * ndelay[...,None] * np.arange(samples) / center
return unwrapped, ndelay

N = N if N is not None else x.shape[-1]
spectrum = np.fft.fft(x, n=N)
spectrum = np.fft.fft(x, n=n)
unwrapped_phase, ndelay = _unwrap(np.angle(spectrum))
log_spectrum = np.log(np.abs(spectrum)) + 1j*unwrapped_phase
ceps = np.fft.ifft(log_spectrum).real

return ceps, ndelay


def real_cepstrum(x, N=None):
def real_cepstrum(x, n=None):
"""Compute the real cepstrum of a real sequence.
x : ndarray
Real sequence to compute real cepstrum of.
N : {None, int}, optional
n : {None, int}, optional
Length of the Fourier transform.
Returns
Expand Down Expand Up @@ -139,8 +138,7 @@ def real_cepstrum(x, N=None):
http://en.wikipedia.org/wiki/Cepstrum
"""
N = N if N is not None else x.shape[-1]
spectrum = np.fft.fft(x, n=N)
spectrum = np.fft.fft(x, n=n)
ceps = np.fft.ifft(np.log(np.abs(spectrum))).real

return ceps
Expand Down Expand Up @@ -203,12 +201,12 @@ def _wrap(phase, ndelay):
return x


def minimum_phase(x, N=None):
def minimum_phase(x, n=None):
"""Compute the minimum phase reconstruction of a real sequence.
x : ndarray
Real sequence to compute the minimum phase reconstruction of.
N : {None, int}, optional
n : {None, int}, optional
Length of the Fourier transform.
Compute the minimum phase reconstruction of a real sequence using the
Expand All @@ -235,11 +233,10 @@ def minimum_phase(x, N=None):
EXPRESS BRIEFS, VOL. 53, NO. 10, OCTOBER 2006
"""
N = N if N is not None else x.shape[-1]
ceps = real_cepstrum(x, N=N)
odd = N % 2
window = np.concatenate(([1.0], 2.0*np.ones((N+odd)/2-1),
np.ones(1-odd), np.zeros((N+odd)/2-1)))
ceps = real_cepstrum(x), n=n)
odd = n % 2
window = np.concatenate(([1.0], 2.0*np.ones((n+odd)/2-1),
np.ones(1-odd), np.zeros((n+odd)/2-1)))

m = np.fft.ifft(np.exp(np.fft.fft(window*ceps))).real

Expand Down

0 comments on commit cb1ad7e

Please sign in to comment.