Splitting piano song .wav file into sections is resulting in the same frequency instead of different frequencies.

1 Ansicht (letzte 30 Tage)
I'm attempting to split a simple audio file into 1 seccond interval sections but the result is yielding the same frequencies. The code is supposed to break up the audio which then performs a fourier transform on the one seccond time interval and yields a frequency.
clear all;
%Parameters
r = 1; %Max and Min Peak Distances
mpd = r*0.9;
MPD = r/0.9;
minpeakheight = 0.2; % Minimum peak height for sampled Piano notes is around 0.2, look at FFT Plot
minpeakdistance = 1;
%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
n = length(y);
t = (1/fs)*(1:length(y));
X = fft(y);
Xk = abs(X);
Xk = Xk(1:length(y)/2);
f = fs*(0:length(y)/2-1)/length(y);
x = f; % x-vector
j = Xk/max(Xk); % y-vector
for k = 1:fs:n
audioBlock = y(k:min(k + fs - 1, n), :);
% do something with this block
[peak_vals,peak_locs] = findpeaks(j,x,'MinPeakHeight',minpeakheight,'MinPeakDistance',minpeakdistance);
[~,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)
audiowrite(name,y,fs);
end

Antworten (1)

Geoff Hayes
Geoff Hayes am 3 Apr. 2020
Voltarian - since you have the audioBlock (of one second's worth of data) wouldn't you just use the code that you already have to do the FFT on y and do that for the block? Something like
for k = 1:fs:n
audioBlock = y(k:min(k + fs - 1, n), :);
% <----- replace y with audioBlock
X = fft(audioBlock);
Xk = abs(X);
Xk = Xk(1:length(audioBlock)/2);
f = fs*(0:length(audioBlock)/2-1)/length(audioBlock);
x = f; % x-vector
j = Xk/max(Xk); % y-vector
[peak_vals,peak_locs] = findpeaks(j,x,'MinPeakHeight',minpeakheight,'MinPeakDistance',minpeakdistance);
[~,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)
% do you really want to rewrite the data to the wav file or write the blocks to
% new files?
% audiowrite(name,y,fs);
end

Kategorien

Mehr zu Audio I/O and Waveform Generation finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by