短时傅立叶变换python

时间:2020-03-20 09:28:35

标签: python numpy fft

我正在尝试在简单的余弦波上进行手动的短时傅立叶变换。我将数据分为8个部分,然后对每个部分应用锥度。 但是我在每个独立的片段上进行傅立叶变换时遇到了麻烦...

到目前为止,这是我的代码:

import numpy as np 

#create a windowing function
def get_windows(n, Mt, olap):
    # Split a signal of length n into olap% overlapping windows each containing Mt terms 
    ists = []
    ieds = []

    ist = 0
    while 1:
        ied = ist + Mt
        if ied > n:
            break
        ists.append(ist)
        ieds.append(ied)
        ist += int(Mt * (1 - olap/100))
    return ists, ieds

#splitting the data into segments by applying the windowing function

fs = 10e3 # Sampling frequency
N = 1e5 # Number of samples
time = np.arange(N) / fs
x = np.cos(5*time)

ists, ieds = get_windows(N, Mt=12500, olap=10) # windows of length 100 and 10% overlap


output = [] 
for ist, ied in zip(ists, ieds):
    output.append(x[ist:ied])


## apply tapering window (hanning window) to make the ends go to 0 of each segment

dim = output.shape

N = dim[1]
n = np.arange(N)
w = np.zeros(N)

for i in range (0,N):
    w[i] = .5*(1 - np.cos((2*np.pi*n[i])/N))


tapered_segments = w[None,:]*output #multiply only with the first axis (slas at different times)


# HERE IS WHERE THE ISSUE COMES UP where i try to perform a FT on the segments

#taking FFT of each segment 

fft = []

for i in range(tapered_segments.shape[0]):
    fft.append(np.fft.rfft(tapered_segments[i,:], axis=0))

fft = np.array(fft)
spectrum = calc_spec(fft)
freqs = freq_arr(tapered_segments[2,:])

请注意,数据(分割成段)的形状为(8,12500),表示有8个数据段的余弦波为12500点。 当我尝试获取每个段的功率谱时...当我尝试在轮廓图中绘制时,我没有输出。 (附有图片)。如何将傅立叶变换应用于数据的每个片段?

original cosine wave

trying to plot the power spectrum

0 个答案:

没有答案