Block Wise FFT on Audio Signal

8 次查看(过去 30 天)
sree charan
sree charan 2021-6-7
编辑: Pramil 2024-4-29
Let us take a audio signal. Divide the audio sequence into non-overlapping block each of 8 samples. Perform fft of each block and concatenate all the fft blocks. Plot the magnitude and phase spectrum.
To verify whether the implementation is correct, divide the fft coefficients into blocks of 8 samples and compute ifft. then compare the ifft with the audio signal. If they are equal then the algorithm is correct.
So, I've tried this code but it's not giving any output
[x,Fs] = audioread('amy.mp3');
x1 = x(:, 1);
a = x1.';
N=8;
for i= 1:27366
temp = a((8*i-7):(8*i));
X=zeros(1, N);
for k = 0:N-1
for n = 0:N-1
X(k+1) = X(k+1)+x(n+1)*exp((-1j*2*pi*k*n)/N);
end
end
Y=zeros(1, N);
for k = 0:N-1
for n = 0:length(x)-1
Y(k+1) = Y(k+1)+(x(n+1)*exp(1i*2*pi*n*k/N));
end
end
end
X;
Y = Y./N;
Z = a - Y

回答(1 个)

Pramil
Pramil 2024-4-29
编辑:Pramil 2024-4-29
Hi Sree,
You can use the built-in MATLAB functions “fft” and “ifft” to address the described problem.
Here is a code that works in MATLAB R2023b for your reference:
[x,Fs] = audioread('song.mp3'); % Load the audio file
x1 = x(:, 1); % Use only one channel if stereo
N = 8; % Block size
% Number of blocks
numBlocks = floor(length(x1)/N);
% Initialize an empty array for FFT results
fftBlocks = zeros(1,numBlocks*8);
% Divide the signal into non-overlapping blocks and compute FFT for each block
for i = 1:numBlocks
block = x1((N*(i-1))+1:N*i); % Extract block
fftBlock = fft(block); % Compute FFT of the block
fftBlocks((N*(i-1))+1:N*i) = fftBlock; % Assign the FFT results
end
% Plot the magnitude and phase spectrum of the concatenated FFT blocks
figure;
subplot(2,1,1);
plot(abs(fftBlocks));
title('Magnitude Spectrum');
subplot(2,1,2);
plot(angle(fftBlocks));
title('Phase Spectrum');
% Verification by computing IFFT
ifftBlocks = zeros(1,numBlocks*8);
for i = 1:numBlocks
block = fftBlocks((N*(i-1))+1:N*i); % Extract block
ifftBlock = ifft(block); % Compute IFFT of the block
ifftBlocks((N*(i-1))+1:N*i) = ifftBlock; % Assign the IFFT results
end
% Compare the original audio signal with the IFFT result (after reshaping it to match original signal length)
originalSignal = x1(1:N*numBlocks); % Original signal might need to be truncated to match the length
reconstructedSignal = reshape(ifftBlocks, [], 1);
% Check if they are equal
difference = originalSignal - reconstructedSignal;
disp(['Difference between original and reconstructed signal: ', num2str(sum(abs(difference)))]);
% A very small difference value indicates that the algorithm is correct.
You can know more about “fft” and “ifft” funtions through the following links:
Hope it helps.

类别

Help CenterFile Exchange 中查找有关 Audio Processing Algorithm Design 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by