Save a matrix to Image and then extract the same matrix from Image
3 ビュー (過去 30 日間)
古いコメントを表示
Greetings everyone.
Above is the link from which I am trying to implement a process. I want to convert stft magnitude of audio to spectrogram and from the spectrogram, I want to regenerate the stft magnitude of audio. Saving the stft magnitude of audio in the spectrogram is easy through imwrite command but I can not recreate the same matrix when I try to convert the spectrogram back to audio. Not sure where I am missing some crucial information that is required to implement this process. Attaching the audio2 file for reference. Audio is attached as a zip file due to file sharing format restrictions. Any help will be highly appreciated.
clear all
close all
clc
[y,Fs]=audioread('audio2.wav');
sound(y,Fs) %To hear the sound
ty = (0:length(y)-1)/Fs;
wind = hamming(128);
olen = 64;
nfft = 1024;
stft(y,Fs,'Window',wind,'OverlapLength',olen,'FFTLength',nfft);
s=stft(y,Fs,'Window',wind,'OverlapLength',olen,'FFTLength',nfft);
smag=abs(s);
sphs=angle(s);
imwrite(smag,'FigureNew',jpg);
imshow(smag);
0 件のコメント
回答 (1 件)
Sumukh
2024 年 9 月 6 日
Hi Fareed.
To create a spectrogram plot for the audio file, the “spectrogram” function in the Signal Processing Toolbox is recommended to be used instead of the “imwrite” function. You can refer to the following documentation to learn more about creating spectrogram plots of audio files:
The “spectrogram” function plots the audio spectrogram along with a colormap for the power spectral density. It also returns a variable “s” containing the “Short-Time Fourier Transform” (STFT) data for the audio signal. The “stft” function also returns this variable without the spectrogram plot. You can learn more about the “stft” function in the following documentation:
The STFT data obtained from either of the above two functions can be passed as input to the “stftmag2sig” function to reconstruct the audio file. You can run the following code to understand how this can be done:
% Read audio file
[y,Fs]=audioread('audio2.wav');
% Input Arguments for spectrogram function
wind = hamming(128);
olen = 64;
nfft = 1024;
% Storing the STFT data in variable "sy" using "spectrogram"
[sy,fy,ty] = spectrogram(y,wind,olen,nfft,[],[],Fs,'centered','power','yaxis');
% Storing the STFT data in variable "sy" using "stft"
%[sy,fy,ty]=stft(y,Fs,'Window',wind,'OverlapLength',olen,'FFTLength',nfft);
% Plotting the spectrogram:
spectrogram(y,wind,olen,nfft,[],[],Fs,'centered','power','yaxis');
% Reconstructing the audio signal from STFT data:
ybar = stftmag2sig(abs(sy),nfft,Fs,'Window',wind,'OverlapLength',olen);
%To hear the original audio sound
% sound(y,Fs);
% To hear reconstructed audio
sound(ybar,Fs);
Minor changes do occur to the reconstructed audio in “ybar” compared to the original audio “y”, as the “spectrogram” or “stft” function attenuates the signal towards the edge of the window and effectively removes some of the energy of the signal. I hope this helps with the issue.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Time-Frequency Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!