Error using findpeaks Expected Y to be a vector.

17 views (last 30 days)
%Parameters
% Read in audio file
[y,Fs] = audioread('Happy Birthday Lower.wav');
info = audioinfo('Happy Birthday Lower.wav');
sound(y,Fs) % Play the sound
% plot the data
% Create a time component using info
t = 0:seconds(1/Fs):seconds(info.Duration);
t = t(1:end-1);
figure(1)
plot(t,y)
xlabel('Time (s)')
ylabel('Audio Signal')
% Detect peaks from y
[pk_Fs, locs_Fs] = findpeaks(y,Fs, 'MinPeakDistance',0.03, 'MinPeakHeight',0.01);
I used this code to try and find the peaks of the given audio signal, the audio signal is extremly simple with no overlapping audio. But this error comes up:
Error using findpeaks
Expected Y to be a vector.
Error in findpeaks>parse_inputs (line 199)
validateattributes(Yin,{'double','single'},{'nonempty','real','vector'},...
Error in findpeaks (line 136)
= parse_inputs(isInMATLAB,Yin,varargin{:});
Error in SeperatePeaks (line 19)
[pk_Fs, locs_Fs] = findpeaks(y,Fs, 'MinPeakDistance',0.03, 'MinPeakHeight',0.01);

Answers (2)

Image Analyst
Image Analyst on 21 Apr 2020
y is probably stereo, so it's a 2-by-N matrix. Try extracting just one channel:
oneChannel = y(1, :); % Or y(:, 1) depending on the shape of y
[pk_Fs, locs_Fs] = findpeaks(oneChannel, Fs, 'MinPeakDistance',0.03, 'MinPeakHeight',0.01);

Star Strider
Star Strider on 21 Apr 2020
The findpeaks function only operates on vectors, and ‘y’ is apparently a (Nx2) matrix.
Try this:
for k =1:size(y,2)
[pk_Fs{k}, locs_Fs{k}] = findpeaks(y(:,k),Fs, 'MinPeakDistance',0.03, 'MinPeakHeight',0.01);
end
That should work. (It generalises in the even that ‘y’ is a vector, as occasionally occurs.)
.

Categories

Find more on Measurements and Spatial Audio 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!