Recover signal from wav file using fft

13 次查看(过去 30 天)
I am trying to use fft function in matlab to recover a signal from a music file Music.wav with lossy compression of CR=0.5 but not able to get it correctly.
Below is my Code
clc;
close all;
clear all;
%reading the given input Music.wav file using audioread()
y = audioread("Music.wav");
%Frame size
size = 2048;
%Creating final dct vectors
yt1 = [];
%looping the input vector frame by frame and getting all the three
%transfored frames from fft() and then taking inverse FFT to get
%the corresponding compressed data
for i=1:size:length(y)-size
yt_dct(:,1) = fft(y(i:i+size-1,1));
yt_dct(:,2) = fft(y(i:i+size-1,2));
yt1(i:i+size-1,1) = real(ifft(yt_dct(1:size/2,1),size));
yt1(i:i+size-1,2) = real(ifft(yt_dct(1:size/2,2),size));
end
%plotting the input file in time domain and in frequency domain
figure;
subplot(211);
plot(y);
title('Music.wav');
xlabel('time(s)');
ylabel('Amplitude');
grid on;
subplot(212);
plot((fft(y)));
title('Frequency spectrum of Music.wav');
xlabel('Frequency (KHz)');
ylabel('Amplitude');
xlim([-7.5,7.5]);
grid on;
%plotting all the music files and the input file in time domain
figure;
subplot(211);
plot(y);
title('Music.wav');
xlabel('time (s)');
ylabel('Amplitude');
grid on;
subplot(212);
plot(yt1);
title('music2.wav with CR = 0.5');
xlabel('time (s)');
ylabel('Amplitude');
grid on;
%plotting all the music files and the input file in Frequency domain
figure;
subplot(211);
plot(fft(y));
title('Frequency spectrum with Music.wav');
xlabel('Frequency (KHz)');
ylabel('Amplitude');
xlim([-7.5, 7.5]);
grid on;
subplot(212);
plot(fft(yt1));
title('Frequency spectrum of music2.wav with CR=0.5');
xlabel('Frequency (KHz)');
ylabel('Amplitude');
grid on;
xlim([-7.5, 7.5]);
%writing the output audio music files with the help of audiowrite
audiowrite('music2.wav',yt1);
The music2.wav file is also not that of expected

采纳的回答

Satwik Samayamantry
Hi KD,
First thing you are missing probably is you have to extract sampling frequency from the music file too for getting x axis data points while plotting. And then while using fft remember that the function fftshift should be used to shift the zero-frequency component of a discrete Fourier transform (DFT) to the center of the spectrum else the spectrum gets shifted which might overlooked and will be a misunderstood as wrong output. The DFT of a signal arranges the frequency components in increasing order from 0 to the Nyquist frequency, followed by negative frequencies from -Nyquist to just below 0. By default, the 0 frequency component is placed at the beginning of the spectrum, which may not be visually intuitive. The fftshift function rearranges the spectrum by circularly shifting the frequency components so that the 0 frequency component is centered in the spectrum. This shift helps in better visualization and analysis of symmetric spectra.
Further help can be done if you can send the music.wav file so that I can run the code.

更多回答(0 个)

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by