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.