App下載

python怎么分離與合并復(fù)數(shù)矩陣的實部虛部

猿友 2021-07-27 11:57:05 瀏覽數(shù) (3824)
反饋

我們知道python的支持復(fù)數(shù),也知道復(fù)數(shù)矩陣的存在,那么我們可以把復(fù)數(shù)矩陣分離成實部和虛部嗎?接下來這篇文章告訴你python怎么分離復(fù)數(shù)矩陣!

需求

在進(jìn)行數(shù)字信號處理的過程中,我們往往有對短時傅里葉變換頻譜(spectrogram)進(jìn)行分析的需求。

常見的分析手段對應(yīng)歐拉公式分為兩種,要么使用模與相位的形式,要么使用實部虛部。

本文分享一個簡單的將復(fù)數(shù)光譜圖分解為實部與虛部以及將兩個部分重新合并為一個復(fù)數(shù)矩陣的過程,以下為python代碼。

import numpy as np
import librosa

 

# load the original wav

test_wave, _ = librosa.load("../RecFile_1_20200617_153719_Sound_Capture_DShow_5_monoOutput1.wav", sr=44100)

# calculate the complex spectrogram stft

spectrogram_test_wav = librosa.stft(test_wave, n_fft=735*2, win_length=735*2, hop_length=735)

 

# calculate the real part of the spectrogram

real_spectrogram = spectrogram_test_wav.real

# calculate the imaginary part of the spectrogram

imaginary_spectrogram = spectrogram_test_wav.imag

 

# combine these two parts

reconstruction_spectrogram = real_spectrogram + 1j * imaginary_spectrogram

print(np.array_equal(spectrogram_test_wav, reconstruction_spectrogram))

其中l(wèi)ibrosa庫為常用的音頻處理庫。

上述代碼實現(xiàn)了對wavfile進(jìn)行短時傅里葉變換,分離出實部虛部并重新合并的過程。

最終的輸出為True, 證明了經(jīng)過這些步驟過后,重構(gòu)的復(fù)數(shù)矩陣與初始的光譜圖是一致的。

以上就是python分離復(fù)數(shù)矩陣的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持W3Cschool。


0 人點贊