xticklabel in the midle of the spectrogram

조회 수: 6 (최근 30일)
Ricardo Duarte
Ricardo Duarte 2023년 10월 4일
편집: Voss 2023년 10월 5일
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.

답변 (1개)

Voss
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
Ricardo Duarte
Ricardo Duarte 2023년 10월 5일
The wav files are too big to upload here
Voss
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 CenterFile Exchange에서 Spectral Analysis에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by