MATLAB: Hey i am new to matlab , actually i want to fft analysis of honey bee sound wav files . i have done with single file now i want to read and plot all files in folder

audiofft

clc
clear
file1='ch01_day05_2a3';
[y,fs] = audioread('C:\Users\Muhammad Yasir\Downloads\Compressed\project matlab\ch01\ch01_day05_2a3.wav');
L=length(y);
t=(1000*(0:1/fs:(L-1)/fs))';
n = 2^nextpow2(L);
Y=fft(y,n);
P = abs(Y/n);
P = P(1:n/2+1);
P(2:end-1) = 2*P(2:end-1);
power = abs(Y).^2/n; % power of the DFT
power = power(1:n/2+1);
power(2:end-1) = 2*power(2:end-1);
f = fs*(0:(n/2))/n;
f1 = fs*(0:n-1)/n;
figure
subplot(4,1,1)
plot(t,y)
xlabel('Time in mseconds');
ylabel('Audio Level');
title('Plot of bee audio files ');
grid on
grid minor
subplot(4,1,2)
plot(f,P)
title('Single-Sided Magnitude Spectrum of bee audio ')
xlabel('f (Hz)')
ylabel('Magnitude')
grid on
grid minor
subplot(4,1,3)
[pk,j] = findpeaks(P,'SortStr','descend','NPeaks',8);
Peaks(:,j) = f(j);
xx=(f(:,j));
xx=(sort(xx));
[~,loc] = max(P);
plot(f(1:10*loc),P(1:10*loc),f(j),pk,'o')
title(['The maixmum of Magnitude Spectrum occurs at ( ' ,num2str(xx) , ' ) Hz' ])
xlabel('f (Hz)')
ylabel('Magnitude')
grid on
grid minor
subplot(4,1,4)
plot(f,power)
xlabel('Frequency')
ylabel('Power')

Best Answer

this worked for me
clc
clear
D = 'C:\Users\Muhammad Yasir\Downloads\Compressed\project matlab\ch08';
S = dir(fullfile(D,'*.wav'));
N = numel(S);
y = cell(1,N);
fs = cell(1,N);
for k = 1:N
F = fullfile(D,S(k).name);
[y{k},fs{k}] = audioread(F);
L=length(y{k});
t=(1000*(0:1/fs{k}:(L-1)/fs{k}));
n = 2^nextpow2(L);
Y=fft(y{k},n);
P = abs(Y/n);
P = P(1:n/2+1);
P(2:end-1) = 2*P(2:end-1);
power = abs(Y).^2/n; % power of the DFT
power = power(1:n/2+1);
power(2:end-1) = 2*power(2:end-1);
f = fs{k}*(0:(n/2))/n;
f1 = fs{k}*(0:n-1)/n;
figure
subplot(4,1,1)
plot(t,y{k})
xlabel('Time in mseconds');
ylabel('Audio Level');
title('Plot of bee audio files ');
grid on
grid minor
subplot(4,1,2)
plot(f,P)
title('Single-Sided Magnitude Spectrum of bee audio ')
xlabel('f (Hz)')
ylabel('Magnitude')
grid on
grid minor
subplot(4,1,3)
[pk,j] = findpeaks(P,'SortStr','descend','NPeaks',8);
Peaks(:,j) = f(j);
xx=(f(:,j));
xx=(sort(xx));
[~,loc] = max(P);
plot(f(1:10*loc),P(1:10*loc),f(j),pk,'o')
title(['The maixmum of Magnitude Spectrum occurs at ( ' ,num2str(xx) , ' ) Hz' ])
xlabel('f (Hz)')
ylabel('Magnitude')
name = sprintf('figure_%d.jpg',k);
% Specify some particular, specific folder:
path = fullfile('C:\Users\Muhammad Yasir\Desktop\ch08\', name);
saveas(gcf,path,'png')
end
% Your destination folder