python 如何將兩個(gè)實(shí)數(shù)矩陣合并為一個(gè)復(fù)數(shù)矩陣
有時(shí)需要把兩個(gè)實(shí)數(shù)矩陣,一個(gè)作為實(shí)部,一個(gè)作為虛部,合并為一個(gè)復(fù)數(shù)矩陣,該如何操作?
解決辦法:假如是在第二個(gè)維度上進(jìn)行合并(real: Data[:, 0, :, :] imag: Data[:, 1, :, :]),有兩種方法
第一種、result = Data[:, 0, :, :] + 1j*Data[:, 1, :, :]第二種、
result = 1j*Data[:, 1, :, :]result += Data[:, 0, :, :]
第二種方法更節(jié)省內(nèi)存~
補(bǔ)充:python numpy 分離與合并復(fù)數(shù)矩陣實(shí)部虛部的方法
在進(jìn)行數(shù)字信號(hào)處理的過程中,我們往往有對(duì)短時(shí)傅里葉變換頻譜(spectrogram)進(jìn)行分析的需求。
常見的分析手段對(duì)應(yīng)歐拉公式分為兩種,要么使用模與相位的形式,要么使用實(shí)部虛部。
本文分享一個(gè)簡(jiǎn)單的將復(fù)數(shù)光譜圖分解為實(shí)部與虛部以及將兩個(gè)部分重新合并為一個(gè)復(fù)數(shù)矩陣的過程,以下為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))
其中l(wèi)ibrosa庫(kù)為常用的音頻處理庫(kù)。
上述代碼實(shí)現(xiàn)了對(duì)wavfile進(jìn)行短時(shí)傅里葉變換,分離出實(shí)部虛部并重新合并的過程。
最終的輸出為True, 證明了經(jīng)過這些步驟過后,重構(gòu)的復(fù)數(shù)矩陣與初始的光譜圖是一致的。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. vue實(shí)現(xiàn)web在線聊天功能2. JavaEE SpringMyBatis是什么? 它和Hibernate的區(qū)別及如何配置MyBatis3. JavaScript實(shí)現(xiàn)頁(yè)面動(dòng)態(tài)驗(yàn)證碼的實(shí)現(xiàn)示例4. Springboot 全局日期格式化處理的實(shí)現(xiàn)5. Java使用Tesseract-Ocr識(shí)別數(shù)字6. 完美解決vue 中多個(gè)echarts圖表自適應(yīng)的問題7. Python使用urlretrieve實(shí)現(xiàn)直接遠(yuǎn)程下載圖片的示例代碼8. SpringBoot+TestNG單元測(cè)試的實(shí)現(xiàn)9. 在Chrome DevTools中調(diào)試JavaScript的實(shí)現(xiàn)10. 解決Android Studio 格式化 Format代碼快捷鍵問題
