python 如何將兩個實數矩陣合并為一個復數矩陣
有時需要把兩個實數矩陣,一個作為實部,一個作為虛部,合并為一個復數矩陣,該如何操作?
解決辦法:假如是在第二個維度上進行合并(real: Data[:, 0, :, :] imag: Data[:, 1, :, :]),有兩種方法
第一種、result = Data[:, 0, :, :] + 1j*Data[:, 1, :, :]第二種、
result = 1j*Data[:, 1, :, :]result += Data[:, 0, :, :]
第二種方法更節省內存~
補充:python numpy 分離與合并復數矩陣實部虛部的方法
在進行數字信號處理的過程中,我們往往有對短時傅里葉變換頻譜(spectrogram)進行分析的需求。
常見的分析手段對應歐拉公式分為兩種,要么使用模與相位的形式,要么使用實部虛部。
本文分享一個簡單的將復數光譜圖分解為實部與虛部以及將兩個部分重新合并為一個復數矩陣的過程,以下為python代碼。
import numpy as npimport librosa# load the original wavtest_wave, _ = librosa.load('../RecFile_1_20200617_153719_Sound_Capture_DShow_5_monoOutput1.wav', sr=44100)# calculate the complex spectrogram stftspectrogram_test_wav = librosa.stft(test_wave, n_fft=735*2, win_length=735*2, hop_length=735)# calculate the real part of the spectrogramreal_spectrogram = spectrogram_test_wav.real# calculate the imaginary part of the spectrogramimaginary_spectrogram = spectrogram_test_wav.imag# combine these two partsreconstruction_spectrogram = real_spectrogram + 1j * imaginary_spectrogramprint(np.array_equal(spectrogram_test_wav, reconstruction_spectrogram))
其中librosa庫為常用的音頻處理庫。
上述代碼實現了對wavfile進行短時傅里葉變換,分離出實部虛部并重新合并的過程。
最終的輸出為True, 證明了經過這些步驟過后,重構的復數矩陣與初始的光譜圖是一致的。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章: