![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1412384/image.png)
Function for getting only 30 peaks per sec of signal not working properly
2 views (last 30 days)
Show older comments
[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 Comments
Accepted Answer
Atithi
on 16 Jun 2023
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');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1412384/image.png)
Got this result as output.
do let me know if it was helpful
More Answers (0)
See Also
Categories
Find more on Spectral Estimation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!