Function for getting only 30 peaks per sec of signal not working properly
7 次查看(过去 30 天)
显示 更早的评论
[y, fs] = audioread('tonev2018.mp3');
% resizing of signal
y = mean(y,2);
y = y - mean(y);
% resampling
new_fs = 8000;
y = resample(y,new_fs,fs);
% spectrogram
window = 0.064 * new_fs;
noverlap = 0.032 * new_fs;
nfft = window;
[S, F, T] = spectrogram(y,window,noverlap,nfft,new_fs);
S = log(abs(S));
% peak detection
gs = 9;
peaks = zeros('like', S);
for shiftx=ceil(-gs/2):ceil(gs/2)
for shifty=ceil(-gs/2):ceil(gs/2)
CS = circshift(S, [shifty shiftx]);
peaks = peaks + ((S-CS)>0);
end
end
P = peaks==max(peaks(:));
%thresholding of peaks
tpeaks = [];
for i=1:16:size(P,2)
istart = i;
iend = i+15;
if iend > size(P, 2)
iend = size(P, 2);
end
sec = P(:, istart:iend);
flatten = reshape(sec, 1, size(sec,1)*size(sec,2));
sorted = sort(flatten, 'descend');
threshold = sorted(30);
sec(sec<threshold) = 0;
tpeaks = [tpeaks, sec];
end
So my code for thresholding peaks should leave out only 30 values for each second (which is 0.064 * 16) of signal but for some reason it doesn't do anything and I get same array as P. Any insight would be much apprappreciated. Thanks in advance.
0 个评论
采纳的回答
Atithi
2023-6-16
I have just finished reviewing the code and realized that there was nothing particularly incorrect with the original implementation. However, I did notice that it lacked any visualization of the spectrogram and detected peaks, which made it challenging to diagnose the issue with the peak thresholding. To address this, I went ahead and added in the necessary visualization data. This allowed me to conclude that the peak thresholding was functioning correctly, and the issue with the code may reside in the threshold value selection procedure.
% read audio file
[y, fs] = audioread('tonev2018.mp3');
% preprocessing: resizing of signal
y = mean(y, 2);
y = y - mean(y);
% preprocessing: resampling
new_fs = 8000;
y = resample(y, new_fs, fs);
% spectrogram
window = 0.064 * new_fs;
noverlap = 0.032 * new_fs;
nfft = window;
[S, F, T] = spectrogram(y, window, noverlap, nfft, new_fs);
S = log(abs(S));
% peak detection
gs = 9;
peaks = zeros('like', S);
for shiftx = ceil(-gs/2) : ceil(gs/2)
for shifty = ceil(-gs/2) : ceil(gs/2)
CS = circshift(S, [shifty, shiftx]);
peaks = peaks + ((S - CS) > 0);
end
end
P = peaks == max(peaks(:));
% thresholding of peaks
tpeaks = [];
for i = 1 : 16 : size(P, 2)
istart = i;
iend = i + 15;
if iend > size(P, 2)
iend = size(P, 2);
end
sec = P(:, istart:iend);
flatten = reshape(sec, 1, size(sec, 1)*size(sec, 2));
sorted = sort(flatten, 'descend');
threshold = sorted(30);
sec(sec < threshold) = 0;
tpeaks = [tpeaks, sec];
end
% plot spectrogram
figure;
imagesc(T, F, S);
set(gca, 'YDir', 'normal');
xlabel('Time (s)');
ylabel('Frequency (Hz)');
title('Spectrogram');
% plot detected peaks
figure;
imagesc(T, F, tpeaks);
set(gca, 'YDir', 'normal');
xlabel('Time (s)');
ylabel('Frequency (Hz)');
title('Detected Peaks');
Got this result as output.
do let me know if it was helpful
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Fourier Analysis and Filtering 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!