def standard_specgram(signal):
"Return specgram matrix, made using the audio-layer config"
return np.array(specgram(signal, NFFT=audioframe_len, noverlap=audioframe_len-audioframe_stride, window=np.hamming(audioframe_len))[0][specbinlow:specbinlow+specbinnum,:], dtype=float32)
python类hamming()的实例源码
def make_wave(self):
"""Inverts the spectrogram and returns a Wave.
returns: Wave
"""
res = []
for t, spectrum in sorted(self.spec_map.items()):
wave = spectrum.make_wave()
n = len(wave)
window = 1 / np.hamming(n)
wave.window(window)
i = wave.find_index(t)
start = i - n // 2
end = start + n
res.append((start, end, wave))
starts, ends, waves = zip(*res)
low = min(starts)
high = max(ends)
ys = np.zeros(high-low, np.float)
for start, end, wave in res:
ys[start:end] = wave.ys
# ts = np.arange(len(ys)) / self.framerate
return Wave(ys, framerate=wave.framerate)
def hamming(self):
"""Apply a Hamming window to the wave.
"""
self.ys *= np.hamming(len(self.ys))
def make_spectrogram(self, seg_length, win_flag=True):
"""Computes the spectrogram of the wave.
seg_length: number of samples in each segment
win_flag: boolean, whether to apply hamming window to each segment
returns: Spectrogram
"""
if win_flag:
window = np.hamming(seg_length)
i, j = 0, seg_length
step = seg_length / 2
# map from time to Spectrum
spec_map = {}
while j < len(self.ys):
segment = self.slice(i, j)
if win_flag:
segment.window(window)
# the nominal time for this segment is the midpoint
t = (segment.start + segment.end) / 2
spec_map[t] = segment.make_spectrum()
i += step
j += step
return Spectrogram(spec_map, seg_length)
def __init__(self, block_length=1024, fft_length=None, step=None,
wfunc=np.hamming, fs=1., donorm=True):
self.block_length = block_length
self.fft_length = fft_length
self.step = step
self.wfunc = wfunc
self.fs = fs
self.donorm = donorm
self.psd = []
def __init__(self, block_length=1024, fft_length=None, step=None,
wfunc=np.hamming, fs=1.):
super(Coherence,
self).__init__(block_length=block_length, fft_length=fft_length,
step=step, wfunc=wfunc, fs=fs)
self.coherence = None
def __init__(self, block_length=1024, fft_length=None, step=None,
wfunc=np.hamming, fs=1.):
super(Bicoherence,
self).__init__(block_length=block_length, fft_length=fft_length,
step=step, wfunc=wfunc, fs=fs)
def local_hamming(vec):
return(hamming(len(vec)))
def FFT(data,rate):
"""given some data points and a rate, return [freq,power]"""
data=data*np.hamming(len(data))
fft=np.fft.fft(data)
fft=10*np.log10(np.abs(fft))
freq=np.fft.fftfreq(len(fft),1/rate)
return freq[:int(len(freq)/2)],fft[:int(len(fft)/2)]
def filterSimMat(simMat, filtLength, filtType, scaleFilterMethod='max1'):
if filtType == 'hamming':
filt = np.hamming(filtLength)
elif filtType == 'flat':
filt = np.ones(filtLength)
else:
raise RuntimeError("Unknown/unsupported filter type {}".format(filtType))
if scaleFilterMethod == 'max1':
filt /= np.max(filt)
elif scaleFilterMethod == 'sum1':
filt /= np.sum(filt)
# print filt
# filt = np.tile(filt, (simMat.shape[0], 1))
# print filt.shape
return filters.convolve1d(simMat, weights=filt, axis=1, mode='constant')
def preprocessFeatureMat(X, Lfilt):
"""
Binarizes and blurs the feature matrix
Parameters
----------
X : 2D array
The original feature matrix (presumably output by buildFeatureMat())
Lfilt : int
The width of the hamming filter used to blur the feature matrix.
Returns
-------
X : 2D array
The modified feature matrix without blur
Xblur : 2D array
The modified feature matrix with blur
"""
Xblur = _filterRows(X, Lfilt)
# ensure that the maximum value in Xblur is 1; we do this by dividing
# by the largets value within Lfilt / 2, rather than just clamping, so
# that there's a smooth dropoff as you move away from dense groups of
# 1s in X; otherwise it basically ends up max-pooled
maxima = filters.maximum_filter1d(Xblur, Lfilt // 2, axis=1, mode='constant')
Xblur[maxima > 0] /= maxima[maxima > 0]
# have columns be adjacent in memory
return np.asfortranarray(X), np.asfortranarray(Xblur)
def _filterRows(X, filtLength):
filt = np.hamming(filtLength)
return filters.convolve1d(X, weights=filt, axis=1, mode='constant')
def filterRows(X, filtLength, filtType='hamming', scaleFilterMethod='max1'):
if filtType == 'hamming':
filt = np.hamming(filtLength)
elif filtType == 'flat':
filt = np.ones(filtLength)
else:
raise RuntimeError("Unknown/unsupported filter type {}".format(filtType))
if scaleFilterMethod == 'max1':
filt /= np.max(filt)
elif scaleFilterMethod == 'sum1':
filt /= np.sum(filt)
return filters.convolve1d(X, weights=filt, axis=1, mode='constant')
def notSoRandomWalk(shape, std=1, trendFilterLength=32, lpfLength=16):
"""bandpass filter a random walk so that the low-frequency trend /
drift is eliminated and the high-frequency noise is attenuated"""
walk = randwalk(shape, std=std)
filt = np.hamming(trendFilterLength)
filt /= np.sum(filt)
whichAxis = len(walk.shape) > 1 # 0 iff 1d, else 1
# subtract baseline drift, roughly
trend = filters.convolve1d(walk, weights=filt, axis=whichAxis, mode='reflect')
walk -= trend
# subtract noisey spikes
walk = filters.convolve1d(walk, weights=np.hamming(lpfLength), axis=whichAxis, mode='reflect')
return walk
def _filterRows(X, filtLength):
filt = np.hamming(filtLength)
return filters.convolve1d(X, weights=filt, axis=1, mode='constant')
def compute_feature_vector(eegdata, Fs):
"""
Extract the features from the EEG
Arguments:
eegdata: array of dimension [number of samples, number of channels]
Fs: sampling frequency of eegdata
Outputs:
feature_vector: np.array of shape [number of feature points; number of different features]
"""
#Delete last column (Status)
eegdata = np.delete(eegdata, -1 , 1)
# 1. Compute the PSD
winSampleLength, nbCh = eegdata.shape
# Apply Hamming window
w = np.hamming(winSampleLength)
dataWinCentered = eegdata - np.mean(eegdata, axis=0) # Remove offset
dataWinCenteredHam = (dataWinCentered.T*w).T
NFFT = nextpow2(winSampleLength)
Y = np.fft.fft(dataWinCenteredHam, n=NFFT, axis=0)/winSampleLength
PSD = 2*np.abs(Y[0:NFFT/2,:])
f = Fs/2*np.linspace(0,1,NFFT/2)
# SPECTRAL FEATURES
# Average of band powers
# Delta <4
ind_delta, = np.where(f<4)
meanDelta = np.mean(PSD[ind_delta,:],axis=0)
# Theta 4-8
ind_theta, = np.where((f>=4) & (f<=8))
meanTheta = np.mean(PSD[ind_theta,:],axis=0)
# Alpha 8-12
ind_alpha, = np.where((f>=8) & (f<=12))
meanAlpha = np.mean(PSD[ind_alpha,:],axis=0)
# Beta 12-30
ind_beta, = np.where((f>=12) & (f<30))
meanBeta = np.mean(PSD[ind_beta,:],axis=0)
feature_vector = np.concatenate((meanDelta, meanTheta, meanAlpha, meanBeta),
axis=0)
feature_vector = np.log10(feature_vector)
return feature_vector
def fft_continuous(data, n=None, psd=False, log='log', fs=None,
window='hamming'):
"""Apply the Fast Fourier Transform on continuous data.
Apply the Fast Fourier Transform algorithm on continuous data to get
the spectrum.
Steps:
1- Demeaning
2- Apply hamming window
3- Compute FFT
4- Grab lower half
Args:
data (numpy.ndarray): shape (`n_samples`, `n_channels`). Data for
which to get the FFT
Keyword Args:
n (int): length of the FFT. If longer than `n_samples`, zero-padding
is used; if smaller, then the signal is cropped. If None, use
the same number as the number of samples
psd (bool): if True, return the Power Spectral Density
log (string): can be 'log' (log10(x)), 'log+1' (log10(x+1)) or None
fs (float): Sampling rate of `data`.
window (string): if 'no_window' do not use a window before
applying the FFT. Otherwise, use as the window function.
Currently only supports 'hamming'.
Returns:
(numpy.ndarray) Fourier Transform of the original signal
(numpy.ndarray): array of frequency bins
"""
if data.ndim == 1:
data = data.reshape((-1, 1))
[n_samples, n_channels] = data.shape
data = data - data.mean(axis=0)
if window.lower() == 'hamming':
H = np.hamming(n_samples).reshape((-1, 1))
elif window.lower() == 'no_window':
H = np.ones(n_samples).reshape((-1, 1))
else:
raise ValueError('window value {} is not supported'.format(window))
L = np.min([n_samples, n]) if n else n_samples
Y = np.fft.fft(data * H, n, axis=0) / L
freq_bins = (fs * np.arange(0, Y.shape[0] / 2 + 1) / Y.shape[0]) \
if fs is not None else None
out = Y[0:int(Y.shape[0] / 2) + 1, :]
out[:, 0] = 2 * out[:, 0]
if psd:
out = np.abs(out) ** 2
if log == 'log':
out = np.log10(out)
elif log == 'log+1':
out = np.log10(out + 1)
return out, freq_bins
def mfcc_extractor(xx, sr, win_len, shift_len, mel_channel, dct_channel, win_type, include_delta):
my_melbank = get_fft_mel_mat(win_len, sr, mel_channel)
pre_emphasis_weight = 0.9375
# x = xx * (1-pre_emphasis_weight)
x = np.append(xx[0], xx[1:] - pre_emphasis_weight * xx[:-1])
dctcoef = np.zeros((dct_channel, mel_channel), dtype=np.float32)
for i in range(dct_channel):
n = np.linspace(0, mel_channel-1, mel_channel)
dctcoef[i, :] = np.cos((2 * n + 1) * i * np.pi / (2 * mel_channel))
w = 1 + 6 * np.sin(np.pi * np.linspace(0, dct_channel-1, dct_channel) / (dct_channel-1))
w /= w.max()
w = np.reshape(w, newshape=(dct_channel, 1))
samples = x.shape[0]
frames = (samples - win_len) // shift_len
stft = np.zeros((win_len, frames), dtype=np.complex64)
spectrum = np.zeros((win_len // 2 + 1, frames), dtype=np.float32)
mfcc = np.zeros((dct_channel, frames), dtype=np.float32)
if win_type == 'hanning':
window = np.hanning(win_len)
elif win_type == 'hamming':
window = np.hamming(win_len)
elif win_type == 'triangle':
window = (1-(np.abs(win_len - 1 - 2*np.arange(1, win_len+1, 1))/(win_len+1)))
else:
window = np.ones(win_len)
for i in range(frames):
one_frame = x[i * shift_len: i * shift_len + win_len]
windowed_frame = np.multiply(one_frame, window)
stft[:, i] = np.fft.fft(windowed_frame, win_len)
spectrum[:, i] = np.power(np.abs(stft[0:win_len // 2 + 1, i]), 2)
c1 = np.matmul(my_melbank, spectrum)
c1 = np.where(c1 == 0.0, np.finfo(float).eps, c1)
mfcc[:dct_channel, :] = np.multiply(np.matmul(dctcoef, np.log(c1)), np.repeat(w, frames, 1))
if include_delta:
dtm = np.zeros((dct_channel, frames), dtype=np.float32)
ddtm = np.zeros((dct_channel, frames), dtype=np.float32)
for i in range(2, frames-2):
dtm[:, i] = 2 * mfcc[:, i+2] + mfcc[:, i+1] - mfcc[:, i-1] - 2 * mfcc[:, i-2]
dtm /= 3.0
for i in range(2, frames-2):
ddtm[:, i] = 2 * dtm[:, i+2] + dtm[:, i+1] - dtm[:, i-1] - 2 * dtm[:, i-2]
ddtm /= 3.0
mfcc = np.row_stack((mfcc[:, 4:frames-4], dtm[:, 4:frames-4], ddtm[:, 4:frames-4]))
return mfcc, spectrum
def mfcc_extractor(xx, sr, win_len, shift_len, mel_channel, dct_channel, win_type, include_delta):
my_melbank = get_fft_mel_mat(win_len, sr, mel_channel)
pre_emphasis_weight = 0.9375
# x = xx * (1-pre_emphasis_weight)
x = np.append(xx[0], xx[1:] - pre_emphasis_weight * xx[:-1])
dctcoef = np.zeros((dct_channel, mel_channel), dtype=np.float32)
for i in range(dct_channel):
n = np.linspace(0, mel_channel-1, mel_channel)
dctcoef[i, :] = np.cos((2 * n + 1) * i * np.pi / (2 * mel_channel))
w = 1 + 6 * np.sin(np.pi * np.linspace(0, dct_channel-1, dct_channel) / (dct_channel-1))
w /= w.max()
w = np.reshape(w, newshape=(dct_channel, 1))
samples = x.shape[0]
frames = (samples - win_len) // shift_len
stft = np.zeros((win_len, frames), dtype=np.complex64)
spectrum = np.zeros((win_len // 2 + 1, frames), dtype=np.float32)
mfcc = np.zeros((dct_channel, frames), dtype=np.float32)
if win_type == 'hanning':
window = np.hanning(win_len)
elif win_type == 'hamming':
window = np.hamming(win_len)
elif win_type == 'triangle':
window = (1-(np.abs(win_len - 1 - 2*np.arange(1, win_len+1, 1))/(win_len+1)))
else:
window = np.ones(win_len)
for i in range(frames):
one_frame = x[i * shift_len: i * shift_len + win_len]
windowed_frame = np.multiply(one_frame, window)
stft[:, i] = np.fft.fft(windowed_frame, win_len)
spectrum[:, i] = np.power(np.abs(stft[0:win_len // 2 + 1, i]), 2)
c1 = np.matmul(my_melbank, spectrum)
c1 = np.where(c1 == 0.0, np.finfo(float).eps, c1)
mfcc[:dct_channel, :] = np.multiply(np.matmul(dctcoef, np.log(c1)), np.repeat(w, frames, 1))
if include_delta:
dtm = np.zeros((dct_channel, frames), dtype=np.float32)
ddtm = np.zeros((dct_channel, frames), dtype=np.float32)
for i in range(2, frames-2):
dtm[:, i] = 2 * mfcc[:, i+2] + mfcc[:, i+1] - mfcc[:, i-1] - 2 * mfcc[:, i-2]
dtm /= 3.0
for i in range(2, frames-2):
ddtm[:, i] = 2 * dtm[:, i+2] + dtm[:, i+1] - dtm[:, i-1] - 2 * dtm[:, i-2]
ddtm /= 3.0
mfcc = np.row_stack((mfcc[:, 4:frames-4], dtm[:, 4:frames-4], ddtm[:, 4:frames-4]))
return mfcc, spectrum