Block Wise FFT on Audio Signal
조회 수: 7 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Audio Processing Algorithm Design에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!