MATLAB Answers

Error using horzcat Dimensions of arrays being concatenated are not consistent. for piano note frequency analysis.

7 views (last 30 days)
Voltarian on 31 Mar 2020
Commented: Rena Berman on 14 May 2020 at 16:19
I have this error that comes up:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in InputAudio (line 26)
y = [y, zeros(1, 1000000)];
The code right now takes a piano note .wav file from the directory and succesfully gives the frequency and pitch of the note. I'm now attempting to add windowing so I can separate frequencies from a simple piano song, the error is on line 26:
y = [y, zeros(1, 1000000)];
Also, if you're curious about
this is just a range of frequencies that is related to a pitch.
This is the Code,
clear all;
N = 512; %Number of points in St-Ft
%User Finds Audio File
[file,path] = uigetfile({'*.wav';},'input file');
name = fullfile(path,file);
%Playing Audio
if isequal(file,0)
disp('User selected Cancel');
disp(['User selected ', fullfile(path,file)]);
[y, fs] = audioread(name);
soundsc(y, fs);
%Fast Fourier Transform
Ns = length(y);
t = (1/fs)*(1:Ns);
%Zero Padding
y = y.*hanning(Ns);
y = [y, zeros(1, 1000000)];
X = fft(y);
Xk = abs(X);
Xk = Xk(1:Ns/2);
f = fs*(0:Ns/2-1)/Ns;
%~~~~~~~~~~~~~~~Plots of Data~~~~~~~~~~~~~~~~%
%Plot Amp. Vs. Time(samples).
plot(t, y,'color','blue')
xlabel('Time (s)')
title('Amplittude over Time')
%FFT Plot
plot(f, Xk/max(Xk))
xlim([0 5000])
xlabel('Frequency (Hz)')
title('Fast Fourier Transform')
%Plot Mag. Vs. Bins.
title('Magnitude of Freq.')
%Find Frequency (Finds Amplitude(y) Peaks which tells Frequency(x))
x = f; % x-vector
y = Xk/max(Xk); % y-vector
minpeakheight = 0.2; % Minimum peak height for sampled Piano notes is around 0.2, look at FFT Plot
minpeakdistance = 1;
[peak_vals,peak_locs] = findpeaks(y,x,'MinPeakHeight',minpeakheight,'MinPeakDistance',minpeakdistance);
r = 1; %Max and Min Peak Distances
mpd = r*0.9;
MPD = r/0.9;
[~,index] = max(peak_vals(peak_locs<peak_locs(1)+mpd)); % index of the frequncy peak
new_peak_locs = peak_locs(index); % position peak/Frequency Value
fprintf('The Frequency of the Note is:')
f = new_peak_locs
new_peak_vals = peak_vals(index);
disp('The Pitch is:')

Answers (1)

Walter Roberson
Walter Roberson on 31 Mar 2020
audioread returns one column per channel. You are attempting to concatenate on a row rather than a column.

Sign in to answer this question.

Translated by