def stft(X, fftsize=128, step="half", mean_normalize=True, real=False,
compute_onesided=True):
"""
Compute STFT for 1D real valued input X
"""
if real:
local_fft = fftpack.rfft
cut = -1
else:
local_fft = fftpack.fft
cut = None
if compute_onesided:
cut = fftsize // 2 + 1
if mean_normalize:
X -= X.mean()
if step == "half":
X = halfoverlap(X, fftsize)
else:
X = overlap(X, fftsize, step)
size = fftsize
win = 0.54 - .46 * np.cos(2 * np.pi * np.arange(size) / (size - 1))
X = X * win[None]
X = local_fft(X)[:, :cut]
return X
python类rfft()的实例源码
def run_mgc_example():
import matplotlib.pyplot as plt
fs, x = wavfile.read("test16k.wav")
pos = 3000
fftlen = 1024
win = np.blackman(fftlen) / np.sqrt(np.sum(np.blackman(fftlen) ** 2))
xw = x[pos:pos + fftlen] * win
sp = 20 * np.log10(np.abs(np.fft.rfft(xw)))
mgc_order = 20
mgc_alpha = 0.41
mgc_gamma = -0.35
mgc_arr = win2mgc(xw, order=mgc_order, alpha=mgc_alpha, gamma=mgc_gamma, verbose=True)
xwsp = 20 * np.log10(np.abs(np.fft.rfft(xw)))
sp = mgc2sp(mgc_arr, mgc_alpha, mgc_gamma, fftlen)
plt.plot(xwsp)
plt.plot(20. / np.log(10) * np.real(sp), "r")
plt.xlim(1, len(xwsp))
plt.show()
def stft(X, fftsize=128, step="half", mean_normalize=True, real=False,
compute_onesided=True):
"""
Compute STFT for 1D real valued input X
"""
if real:
local_fft = fftpack.rfft
cut = -1
else:
local_fft = fftpack.fft
cut = None
if compute_onesided:
cut = fftsize // 2 + 1
if mean_normalize:
X -= X.mean()
if step == "half":
X = halfoverlap(X, fftsize)
else:
X = overlap(X, fftsize, step)
size = fftsize
win = 0.54 - .46 * np.cos(2 * np.pi * np.arange(size) / (size - 1))
X = X * win[None]
X = local_fft(X)[:, :cut]
return X
def run_mgc_example():
import matplotlib.pyplot as plt
fs, x = wavfile.read("test16k.wav")
pos = 3000
fftlen = 1024
win = np.blackman(fftlen) / np.sqrt(np.sum(np.blackman(fftlen) ** 2))
xw = x[pos:pos + fftlen] * win
sp = 20 * np.log10(np.abs(np.fft.rfft(xw)))
mgc_order = 20
mgc_alpha = 0.41
mgc_gamma = -0.35
mgc_arr = win2mgc(xw, order=mgc_order, alpha=mgc_alpha, gamma=mgc_gamma, verbose=True)
xwsp = 20 * np.log10(np.abs(np.fft.rfft(xw)))
sp = mgc2sp(mgc_arr, mgc_alpha, mgc_gamma, fftlen)
plt.plot(xwsp)
plt.plot(20. / np.log(10) * np.real(sp), "r")
plt.xlim(1, len(xwsp))
plt.show()
def stft(X, fftsize=128, step="half", mean_normalize=True, real=False,
compute_onesided=True):
"""
Compute STFT for 1D real valued input X
"""
if real:
local_fft = fftpack.rfft
cut = -1
else:
local_fft = fftpack.fft
cut = None
if compute_onesided:
cut = fftsize // 2
if mean_normalize:
X -= X.mean()
if step == "half":
X = halfoverlap(X, fftsize)
else:
X = overlap(X, fftsize, step)
size = fftsize
win = 0.54 - .46 * np.cos(2 * np.pi * np.arange(size) / (size - 1))
X = X * win[None]
X = local_fft(X)[:, :cut]
return X
def stft(X, fftsize=128, step="half", mean_normalize=True, real=False,
compute_onesided=True):
"""
Compute STFT for 1D real valued input X
"""
if real:
local_fft = fftpack.rfft
cut = -1
else:
local_fft = fftpack.fft
cut = None
if compute_onesided:
cut = fftsize // 2
if mean_normalize:
X -= X.mean()
if step == "half":
X = halfoverlap(X, fftsize)
else:
X = overlap(X, fftsize, step)
size = fftsize
win = 0.54 - .46 * np.cos(2 * np.pi * np.arange(size) / (size - 1))
X = X * win[None]
X = local_fft(X)[:, :cut]
return X
def stft(X, fftsize=128, step="half", mean_normalize=True, real=False,
compute_onesided=True):
"""
Compute STFT for 1D real valued input X
"""
if real:
local_fft = fftpack.rfft
cut = -1
else:
local_fft = fftpack.fft
cut = None
if compute_onesided:
cut = fftsize // 2
if mean_normalize:
X -= X.mean()
if step == "half":
X = halfoverlap(X, fftsize)
else:
X = overlap(X, fftsize, step)
size = fftsize
win = 0.54 - .46 * np.cos(2 * np.pi * np.arange(size) / (size - 1))
X = X * win[None]
X = local_fft(X)[:, :cut]
return X
def stft(X, fftsize=128, step="half", mean_normalize=True, real=False,
compute_onesided=True):
"""
Compute STFT for 1D real valued input X
"""
if real:
local_fft = fftpack.rfft
cut = -1
else:
local_fft = fftpack.fft
cut = None
if compute_onesided:
cut = fftsize // 2
if mean_normalize:
X -= X.mean()
if step == "half":
X = halfoverlap(X, fftsize)
else:
X = overlap(X, fftsize, step)
size = fftsize
win = 0.54 - .46 * np.cos(2 * np.pi * np.arange(size) / (size - 1))
X = X * win[None]
X = local_fft(X)[:, :cut]
return X
def stft(X, fftsize=128, step="half", mean_normalize=True, real=False,
compute_onesided=True):
"""
Compute STFT for 1D real valued input X
"""
if real:
local_fft = fftpack.rfft
cut = -1
else:
local_fft = fftpack.fft
cut = None
if compute_onesided:
cut = fftsize // 2
if mean_normalize:
X -= X.mean()
if step == "half":
X = halfoverlap(X, fftsize)
else:
X = overlap(X, fftsize, step)
size = fftsize
win = 0.54 - .46 * np.cos(2 * np.pi * np.arange(size) / (size - 1))
X = X * win[None]
X = local_fft(X)[:, :cut]
return X
def stft(X, fftsize=128, step="half", mean_normalize=True, real=False,
compute_onesided=True):
"""
Compute STFT for 1D real valued input X
"""
if real:
local_fft = fftpack.rfft
cut = -1
else:
local_fft = fftpack.fft
cut = None
if compute_onesided:
cut = fftsize // 2
if mean_normalize:
X -= X.mean()
if step == "half":
X = halfoverlap(X, fftsize)
else:
X = overlap(X, fftsize, step)
size = fftsize
win = 0.54 - .46 * np.cos(2 * np.pi * np.arange(size) / (size - 1))
X = X * win[None]
X = local_fft(X)[:, :cut]
return X
def stft(X, fftsize=128, step="half", mean_normalize=True, real=False,
compute_onesided=True):
"""
Compute STFT for 1D real valued input X
"""
if real:
local_fft = fftpack.rfft
cut = -1
else:
local_fft = fftpack.fft
cut = None
if compute_onesided:
cut = fftsize // 2
if mean_normalize:
X -= X.mean()
if step == "half":
X = halfoverlap(X, fftsize)
else:
X = overlap(X, fftsize, step)
size = fftsize
win = 0.54 - .46 * np.cos(2 * np.pi * np.arange(size) / (size - 1))
X = X * win[None]
X = local_fft(X)[:, :cut]
return X
def stft(X, fftsize=128, step="half", mean_normalize=True, real=False,
compute_onesided=True):
"""
Compute STFT for 1D real valued input X
"""
if real:
local_fft = fftpack.rfft
cut = -1
else:
local_fft = fftpack.fft
cut = None
if compute_onesided:
cut = fftsize // 2
if mean_normalize:
X -= X.mean()
if step == "half":
X = halfoverlap(X, fftsize)
else:
X = overlap(X, fftsize, step)
size = fftsize
win = 0.54 - .46 * np.cos(2 * np.pi * np.arange(size) / (size - 1))
X = X * win[None]
X = local_fft(X)[:, :cut]
return X
def stft(X, fftsize=128, step="half", mean_normalize=True, real=False,
compute_onesided=True):
"""
Compute STFT for 1D real valued input X
"""
if real:
local_fft = fftpack.rfft
cut = -1
else:
local_fft = fftpack.fft
cut = None
if compute_onesided:
cut = fftsize // 2
if mean_normalize:
X -= X.mean()
if step == "half":
X = halfoverlap(X, fftsize)
else:
X = overlap(X, fftsize, step)
size = fftsize
win = 0.54 - .46 * np.cos(2 * np.pi * np.arange(size) / (size - 1))
X = X * win[None]
X = local_fft(X)[:, :cut]
return X
def stft(X, fftsize=128, step="half", mean_normalize=True, real=False,
compute_onesided=True):
"""
Compute STFT for 1D real valued input X
"""
if real:
local_fft = fftpack.rfft
cut = -1
else:
local_fft = fftpack.fft
cut = None
if compute_onesided:
cut = fftsize // 2
if mean_normalize:
X -= X.mean()
if step == "half":
X = halfoverlap(X, fftsize)
else:
X = overlap(X, fftsize, step)
size = fftsize
win = 0.54 - .46 * np.cos(2 * np.pi * np.arange(size) / (size - 1))
X = X * win[None]
X = local_fft(X)[:, :cut]
return X
def stft(X, fftsize=128, step="half", mean_normalize=True, real=False,
compute_onesided=True):
"""
Compute STFT for 1D real valued input X
"""
if real:
local_fft = fftpack.rfft
cut = -1
else:
local_fft = fftpack.fft
cut = None
if compute_onesided:
cut = fftsize // 2
if mean_normalize:
X -= X.mean()
if step == "half":
X = halfoverlap(X, fftsize)
else:
X = overlap(X, fftsize, step)
size = fftsize
win = 0.54 - .46 * np.cos(2 * np.pi * np.arange(size) / (size - 1))
X = X * win[None]
X = local_fft(X)[:, :cut]
return X
def amp_freq(timeSeries):
freq = spfft.rfftfreq(len(timeSeries),1)
fft = spfft.rfft(timeSeries)
amp = np.abs(fft)
indmax = np.where(amp==np.max(amp))[0][0]
oi = (np.sum(amp[indmax-1:indmax+1]))/np.sum(amp)
indmax = np.where(amp==np.max(amp))[0][0]
dfreq = freq[indmax]*1000 # Sampling frequency = 1/1ms = 1/0.001= 1000
return oi,dfreq
def bandpass(data_list, min_hz, max_hz):
fft_list = rfft(data_list)
# Filter
for i in range(len(fft_list)):
if not (min_hz < i/2+1 < max_hz): fft_list[i] = 0
result_vals = irfft(fft_list)
return result_vals
def remove_freq_range(data_list, min_hz, max_hz):
fft_list = rfft(data_list)
# Filter
for i in range(len(fft_list)):
if (min_hz < i / 2 + 1 < max_hz): fft_list[i] = 0
result_vals = irfft(fft_list)
return result_vals
data_load.py 文件源码
项目:Automatic-feature-extraction-from-signal
作者: VVVikulin
项目源码
文件源码
阅读 34
收藏 0
点赞 0
评论 0
def calc_fft(data):
return rfft(data)
data_load.py 文件源码
项目:Automatic-feature-extraction-from-signal
作者: VVVikulin
项目源码
文件源码
阅读 32
收藏 0
点赞 0
评论 0
def high_filter(data, sample_rate=1000):
f_signal = rfft(data)
l = int(len(f_signal)*50.0/sample_rate);
cut_f_signal = f_signal.copy()
cut_f_signal[l:len(f_signal)-1] = 0
cut_signal = irfft(cut_f_signal)
return cut_signal
data_load.py 文件源码
项目:Automatic-feature-extraction-from-signal
作者: VVVikulin
项目源码
文件源码
阅读 33
收藏 0
点赞 0
评论 0
def low_filter(data, sample_rate=1000):
f_signal = rfft(data)
l = int(len(f_signal)*5.0/sample_rate);
cut_f_signal = f_signal.copy()
cut_f_signal[0:l+1] = 0
cut_f_signal[len(f_signal) + 1 - l :] = 0
cut_signal = irfft(cut_f_signal)
return cut_signal
data_load.py 文件源码
项目:Automatic-feature-extraction-from-signal
作者: VVVikulin
项目源码
文件源码
阅读 36
收藏 0
点赞 0
评论 0
def simple_lf_filter(signal, sample_rate=1000):
W = fftfreq(signal.size, d=1/float(sample_rate))
f_signal = rfft(signal)
cut_f_signal = f_signal.copy()
cut_f_signal[(W<5)] = 0
return irfft(cut_f_signal).astype('int16')
data_load.py 文件源码
项目:Automatic-feature-extraction-from-signal
作者: VVVikulin
项目源码
文件源码
阅读 34
收藏 0
点赞 0
评论 0
def simple_hf_filter(signal, sample_rate=1000):
W = fftfreq(signal.size, d=1/float(sample_rate))
f_signal = rfft(signal)
cut_f_signal = f_signal.copy()
cut_f_signal[(W>70)] = 0
return irfft(cut_f_signal).astype('int16')
def _get_curve(self):
"""
No transfer_function correction
:return:
"""
iq_data = self._get_filtered_iq_data() # get iq data (from scope)
if not self.running_state in ["running_single", "running_continuous"]:
self.pyrpl.scopes.free(self.scope) # free scope if not continuous
if self.baseband:
# In baseband, where the 2 real inputs are stored in the real and
# imaginary part of iq_data, we need to make 2 different FFTs. Of
# course, we could do it naively by calling twice fft, however,
# this is not optimal:
# x = rand(10000)
# y = rand(10000)
# %timeit fftpack.fft(x) # --> 74.3 us (143 us with numpy)
# %timeit fftpack.fft(x + 1j*y) # --> 163 us (182 us with numpy)
# A convenient option described in Oppenheim/Schafer p.
# 333-334 consists in taking the right combinations of
# negative/positive/real/imaginary part of the complex fft,
# however, an optimized function for real FFT is already provided:
# %timeit fftpack.rfft(x) # --> 63 us (72.7 us with numpy)
# --> In fact, we will use numpy.rfft insead of
# scipy.fftpack.rfft because the output
# format is directly a complex array, and thus, easier to handle.
fft1 = np.fft.fftpack.rfft(np.real(iq_data),
self.data_length*self.PADDING_FACTOR)
fft2 = np.fft.fftpack.rfft(np.imag(iq_data),
self.data_length*self.PADDING_FACTOR)
cross_spectrum = np.conjugate(fft1)*fft2
res = np.array([abs(fft1)**2,
abs(fft2)**2,
np.real(cross_spectrum),
np.imag(cross_spectrum)])
# at some point, we need to cache the tf for performance
self._last_curve_raw = res # for debugging purpose
return res/abs(self.transfer_function(self.frequencies))**2
else:
# Realize the complex fft of iq data
res = scipy.fftpack.fftshift(scipy.fftpack.fft(iq_data,
self.data_length*self.PADDING_FACTOR))
# at some point we need to cache the tf for performance
self._last_curve_raw = np.abs(res)**2 # for debugging purpose
return self._last_curve_raw/abs(self.transfer_function(
self.frequencies))**2
#/ abs(self.transfer_function(
#self.frequencies))**2
# [self.useful_index()]