0
$\begingroup$

In this code, i am doing a STFT on my audio file. I want to find the rhythm of file. Bascially the bass, which is located in the lower frequencies. I think i need to find the BPM of my file. The tempo of a song is measured by BPM, which is i need. There is a method,that if there's strong beat is to take the magnitude of the STFT of the waveform, and then auto-correlate it in only the time dimension. The peak of the auto-correlation function will be the beat, or a submultiple of it.

This is equivalent to breaking up the signal into a lot of different frequency bands, finding the amplitude envelope of each, autocorrelating each envelope, and then summing them. The noise and other parts of music are averaged out by the cross-correlation operation.

I tried to find this method, how excatly this can be performed. I was searching for an open-source-code or any kind of instructions, how this can be done. But i wasn't able to find anything. Does anyone can help me to reach the steps explained in the methods?

%% MATLAB
%% STFT = SHORT TIME FOURIER-TRANSFORMATION
%___________________________________________
clear all;
[y,fs]=audioread('UnchainMyHeart.wav');
% audioread = Reading the WAV-File 
% y = A vector, which contains our audio signal
% fs = Sampling frequency
% 'UnchainMyHeart' = Name of the WAV-File

%PARAMETER FOR THE CODE
%_____________________________________________
t_seg=0.09; %Length of the segment, on which we use the STFT

fftlen = 4096; 
% Length of the fft

% L,M,H bands
% Start & Stop frequency defenition of bands
fL = [  1,  300 ]; % Low frequencies
fM = [  301,  5000 ]; % Medium frequencies
fH = [ 5001, 22050 ]; % High frequencies
%_____________________________________________

segl =floor(t_seg*fs); 
% Length of the segment(50ms), which is being multiplied by the       
% sampling frequency 
% The result is rounded off with the function "floor"

windowshift=segl/3; 
% Defining the size of the window, which goes to next segment and so 
% on,until to the end of the audio signal

window=hann(segl); 
% Hanning Function, which is being stored in a vraiable
% Matlab usually works better with variables than actual numbers. Hence 
% the workspace. This way, you can avoid errors.  

window=window.'; 
% transpose vector from a line vector to a row vector
% In the workspace, you can see, what kind of vector it is

si=1; 
% Start index

ei=segl; 
% End index

AOS=floor( length(y)/windowshift - 1);
% Determining the numbers of segments in my audio signal


f1=figure;
% New figure is being openend
n=0:1:fftlen-1;
f=n/(fftlen-1)*fs;
% Definig frequency vector

Ya=zeros(1,fftlen);
% The values between "1" and "fftlen" are being filled with zeros

for m= 1:1:AOS

y_a = y(si:ei);
y_a= y_a.*window;
Ya=fft(y_a, fftlen);
YA=abs(Ya);

drawnow; 
% Updates the graphical objects

% indices of bands
LI = find((f>=fL(1))&(f<=fL(2))); 
MI = find((f>=fM(1))&(f<=fM(2)));
HI = find((f>=fH(1))&(f<=fH(2)));
% mean values of bands
ML = mean(YA(LI));
MM = mean(YA(MI));
MH = mean(YA(HI));

figure(f1);
plot(f, 20*log10(YA));    
ylim([-90 50]);
xlim([0 fs/2]);

% Limiting the length of my y-axis
title('Spectrum of audio signal');
% Title
xlabel('f(Hz)');
% Name of x-axis
ylabel('abs(Ya)/dB');
% Name of y-axis
grid on;
% Generating grid raster

si=si+windowshift; 
%Start index is being updated
ei=ei+windowshift; 
%End index is being updated

end
$\endgroup$
1
$\begingroup$

You should be more clear about Detecting Bass.

Bass instrument detection (i.e. polyphonic pitch tracking) and Bass Drum detection (to compute "rhythmic features" or tempo/BPM) are two different tasks. I'll assume you're talking about the latter.

I recommend looking at the open source music-analysis library called librosa, you can do tempo analysis in one line, on audio you've read into memory. You can also use the computed beats to compute various other beat-synchronous features:

import librosa
y, sr = librosa.load(librosa.util.example_audio_file(),sr=None)
tempo, beats = librosa.beat.beat_track(y=y, sr=sr)
print(tempo)
print(beats)

Links to digest:

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.