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?

clear all;
% audioread = Reading the WAV-File 
% y = A vector, which contains our audio signal
% fs = Sampling frequency
% 'UnchainMyHeart' = Name of the WAV-File

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"

% Defining the size of the window, which goes to next segment and so 
% on,until to the end of the audio signal

% 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.  

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

% Start index

% End index

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

% New figure is being openend
% Definig frequency vector

% 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);

% 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));

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
% Name of x-axis
% Name of y-axis
grid on;
% Generating grid raster

%Start index is being updated
%End index is being updated


1 Answer 1


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)

Links to digest:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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