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

1 view (last 30 days)
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
FrequencyToPitch(f)
this is just a range of frequencies that is related to a pitch.
This is the Code,
clear all;
%Parameters
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');
else
disp(['User selected ', fullfile(path,file)]);
[y, fs] = audioread(name);
soundsc(y, fs);
end
%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).
figure('color','white')
plot(t, y,'color','blue')
xlabel('Time (s)')
ylabel('Amplitude')
title('Amplittude over Time')
%FFT Plot
figure('color','white')
plot(f, Xk/max(Xk))
xlim([0 5000])
xlabel('Frequency (Hz)')
ylabel('Amplitude')
title('Fast Fourier Transform')
%Plot Mag. Vs. Bins.
figure('color','white')
plot([0:length(X)-1],abs(X))
xlabel('Bins')
ylabel('Magnitude(Freq.)')
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
%DisplayFrequency
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);
%DisplayPitch
disp('The Pitch is:')
FrequencyToPitch(f)

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.

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!