欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

根据公式解析离散傅立叶

程序员文章站 2022-06-13 21:14:13
import numpy as npimport matplotlib.pyplot as pltdef signal_xHz(A, fi, time_s, sample): print(fi * time_s * 2 * np.pi) print(sample* time_s) return A * np.sin(np.linspace(0, fi * time_s * 2 * np.pi , num=sample* time_s))sig = signal_xHz(2...
import numpy as np
import matplotlib.pyplot as plt

def signal_xHz(A, fi, time_s, sample):
    print(fi * time_s * 2 * np.pi)
    print(sample* time_s)
    return A * np.sin(np.linspace(0, fi * time_s * 2 * np.pi , num=sample* time_s))
sig = signal_xHz(20000,1000,5,8000)
31415.926535897932
40000
plt.plot(sig[0:10])
[<matplotlib.lines.Line2D at 0x11bbba3d0>]

根据公式解析离散傅立叶

sig[0:10]
array([ 0.00000000e+00,  1.41424133e+04,  2.00000000e+04,  1.41413025e+04,
       -1.57083560e+00, -1.41435240e+04, -1.99999999e+04, -1.41401917e+04,
        3.14167118e+00,  1.41446346e+04])
type(sig)
sig.astype('int16').tofile('sin.pcm')
import os
os.system('sox -t raw -c 1 -e signed-integer -b 16 -r 8000 sin.pcm sin.wav')
os.system('play sin.wav')
0
print (sig.astype('int16')[0:100])
[     0  14142  19999  14141     -1 -14143 -19999 -14140      3  14144
  19999  14139     -4 -14145 -19999 -14137      6  14146  19999  14136
     -7 -14147 -19999 -14135      9  14149  19999  14134    -10 -14150
 -19999 -14133     12  14151  19999  14132    -14 -14152 -19999 -14131
     15  14153  19999  14130    -17 -14154 -19999 -14129     18  14155
  19999  14127    -20 -14156 -19999 -14126     21  14157  19999  14125
    -23 -14159 -19999 -14124     25  14160  19999  14123    -26 -14161
 -19999 -14122     28  14162  19999  14121    -29 -14163 -19999 -14120
     31  14164  19999  14119    -32 -14165 -19999 -14117     34  14166
  19999  14116    -36 -14167 -19999 -14115     37  14169  19999  14114]

ak=2Ni=0N1xicos2kiπNa_{k}=\frac{2}{N}\sum_{i=0}^{N-1}{x_{i}cos\frac{2ki\pi}{N}}

ak=0
N=8000
k=1000
for i in np.arange(0,8000-1):
    ak+=sig[i]*np.cos(2*np.pi*k*i/N)
print(ak/N)
784.6553321430675

ck=1Ni=0N1xiej2kiπNc_{k}=\frac{1}{N}\sum_{i=0}^{N-1}{x_{i}e^{-j\frac{2ki\pi}{N}}}

ak=0
N=8000
k=1000
for i in np.arange(0,8000-1):
    ak+=sig[i]*np.exp(2*np.pi*k*i*(-1.j)/N)
print(ak/N)
(784.6553321424174-9957.788439085387j)
y=sig[0:8000]
YY = np.fft.fft(y)   # 未归一化
Y = np.fft.fft(y)/len(y)   # fft computing and normalization 归一化
Y1 = Y[range(int(len(y)/2))]
print(Y[999:1001])
[ 18.92979144 -242.80867967j 783.61624233-9958.8275289j ]
plt.plot(Y1)
/Users/chenpeiwen/opt/anaconda3/lib/python3.7/site-packages/numpy/core/_asarray.py:85: ComplexWarning: Casting complex values to real discards the imaginary part
  return array(a, dtype, copy=False, order=order)

根据公式解析离散傅立叶

[<matplotlib.lines.Line2D at 0x11bd3d050>]
import librosa
import librosa.display
y, sr = librosa.load('sin.wav')
S = np.abs(librosa.stft(y))
plt.figure()
librosa.display.specshow(S**2, sr=sr, y_axis='log')  # 从波形获取功率谱图
plt.colorbar()
plt.title('Power spectrogram')

Text(0.5, 1.0, 'Power spectrogram')

根据公式解析离散傅立叶

mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=40)
print(mfccs.shape)        # (40, 65)
(40, 216)

本文地址:https://blog.csdn.net/daad777/article/details/107122588