xticklabel in the midle of the spectrogram
3 次查看(过去 30 天)
显示 更早的评论
Hello all,
I have several .wav files with 3minutes each that I used to compute a spectrogram.
Now I want to put the name of each .WAV file in the xticklabels in the midle of the correspondent part of the espectrogram.
Does anyone have an idea how this can be done?
Thank you in advance.
0 个评论
回答(1 个)
Voss
2023-10-4
Something like this maybe:
% some plot
plot(rand(1,1000));
% some file names:
name = {'a wav file.wav','another wav file.wav'};
% some region boundaries along the x-axis
x = [0 400 1000];
xline(x(2:end-1),'r','LineWidth',2); % so you can see the boundary/ies
% set xticks in the middle of each region, labeled according to 'name'
set(gca(),'XTick',(x(1:end-1)+x(2:end))/2,'XTickLabel',name);
5 个评论
Voss
2023-10-5
编辑:Voss
2023-10-5
You can keep track of the size of each wav data set (the number of samples) when you read each file. That is the row vector n_samples below. Then the boundaries are x = cumsum([0 n_samples]/Fs);
Si=-174;
concatenatedAudio = [];
tic;
n_files = numel(fileList);
n_samples = zeros(1,n_files);
%Loop through each audio file
for i = 1:n_files
%Load the audio file
[audio, Fs] = audioread(fileList(i).name);
% keep track of the size of each data set:
n_samples(i) = size(audio,1);
allDates{i} = fileList(i).date;
%Concatenate the audio data
concatenatedAudio = [concatenatedAudio; audio];
end
concatenatedAudio=single(concatenatedAudio);
%% Spectrogram calculation
% Example spectrogram parameters
if strcmp(windowType,'Hann')
window = (0.5 - 0.5*cos(2*pi*(1:Fs)/Fs));%hann
elseif strcmp(windowType,'Hamming')
window = (0.54 - 0.46*cos(2*pi*(1:Fs)/Fs)); %hamming
end
[S, F, T, P] = spectrogram(concatenatedAudio, window, [], [lowfreq:Fs/2] ,Fs);
% Plot the spectrogram
PdB=10*log10(abs(P'))-Si;
figure; surf(T,F,PdB'); view(2); shading flat;set(gca,'YScale','log')
% set the xticks and xticklabels
x = cumsum([0 n_samples]/Fs);
set(gca(),'XTick',(x(1:end-1)+x(2:end))/2,'XTickLabel',{fileList.name});
colormap('parula'); % Choose a colormap
title('Spectrogram of Combined Audio Files');
xlabel('Time (s)');
ylabel('Frequency (Hz)');
colorbar
ax = gca; % axes handle
ax.YAxis.Exponent = 0;
xlim([T(1) T(end)]);
ylim([F(1) F(end)]);
caxis([0 max(max(PdB))]);
ylabel(colorbar,['PSD [ dB re ' num2str(1) ' \muP'...
'a^2 Hz^-^1 ]'],'fontname','arial','fontsize',14);
PSDTime=toc;
fprintf('PSD time elapsted: %s',num2str(PSDTime))
fprintf('s','\n')
fprintf('\n')
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Simulation, Tuning, and Visualization 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!