2
$\begingroup$

Hi guys i am doing a course in Digital Filters and Spectral analysis. We are given a coursework/homework, and I have absolutely no idea what to do with it. I come from Maths background, never done any signal processing before, and since I am new to the university I don't really have anyone to ask.

Problem:

In this exercise you are required to use spectral analysis techniques to determine the musical notes played within a short audio sample (with sampling frequency 44.1KHz). The sample will comprise a short sequence of 5 chords, each comprising 3 or 4 different musical notes played concurrently. Each note comprises a fundamental plus a series of harmonics at multiples of the fundamental frequency. All of the notes in this exercise belong to the 12 note equal temperament scale.

Here is some piece of code I scrambled so far:

[x,fs] = audioread('sample1_va18535.wav');
fs % fs is the sampling frequency usually 44.1 KHz
wavplay; % Play audio
N = 4410; % 0.1 seconds at 44.1KHz
N1 = 2205; % 0.05 seconds
n = N1+1:N1+N; % 0.05 – 0.15 seconds
xn = x(n,1); % Select left channel of short clip
wavplay; % Play short clip
t = n/fs; % Time index
plot(t,xn);
xlabel('Time (s)');
ylabel('Amplitude');
window = hamming(N); % Create window
wxn = xn .* window; % Apply window
Xk = fft(wxn); % DFT
k = 0:N-1;
f=k*fs/N; % Frequency in Hz
pause;
plot(f,abs(Xk));
ylabel('Magnitude');
xlabel('Frequency (Hz)');

Now I am pretty sure this is far from over ( I am not even sure if this code is correct).Would anyone be so kind and explain to me how can I generate those frequencies? I know I am required to create a vector of fundamental frequencies and ignore any harmonics.

Here is the file: https://ufile.io/lyge5

$\endgroup$
14
  • $\begingroup$ Hi Kudera. Is it possible that you could also upload the audio file ? (if not large) $\endgroup$
    – Fat32
    Commented Dec 11, 2018 at 20:38
  • $\begingroup$ tell your professor that using the FFT for musical note detection is a bad idea. $\endgroup$ Commented Dec 11, 2018 at 20:40
  • $\begingroup$ Hi fat32. i definitely could. it is very short but I am not sure how? and Robert, the way this module is delivered is terrible. We have a lab for every other module which at least give some instructions on how to do stuff. Not for this one. $\endgroup$ Commented Dec 11, 2018 at 20:45
  • 1
    $\begingroup$ @Rikki The fundamental method for pitch estimation of monophonic musical tones is based on the Average Squared Difference Function with some modification so that it looks like the autocorrelation function. Then it is a matter of peak picking. This can be laborious for the computer or DSP, but it can be done in the time domain without FFT. $\endgroup$ Commented Aug 21, 2021 at 20:30
  • 1
    $\begingroup$ Thank you @robertbristow-johnson! I very much appreciate it 😀 $\endgroup$
    – Rikki
    Commented Aug 23, 2021 at 4:50

2 Answers 2

3
$\begingroup$

As Robert B.J. has already indicated, a bare bones FFT analysis is not the recommended method for a professional audio harmonic inspection. Nevertheless it can be very useful in certain cases, one of which is, I think, this one. Be aslo warned that, as hotpaw2 indicated, with this simplistic approach, false positives might be detected.

From your provided file (45000+ samples, 1 second of duration and taken at 16 bits, 44100 Hz), by first by plotting it you will notice that there are 5 (almost) equal length pieces each about 9000 samples long. These 5 pieces correspond to those 5 chords played and defined in the question. As shown in the plot below:

enter image description here

Now, for simplicity of the analysis, I've only taken the first chord block of 9000 samples and computed its periodogram ($\frac{1}{N}|X(\omega)|^2$) wrt Hertz frequency. The result is the following figure.

enter image description here

From this spectral plot, you can clearly see those harmonic spikes. What you need to do is to find the frequencies corresponding to each of those peaks; i.e., find the abscissa corresponding to the peaks. You can use a number of algorithms for (precisely) finding those peak frequencies. But for a basic analysis you don't need scientific accuracy. Here is the set of (approximate) frequencies I've found to be existing on this spectral plot:

$$f = \{176, 221, 262, 350, 441, 525, 660, 700, 786, 874, 881, 1048, 1101, 1223, 1309, 1321, 1398, 1541, 1571, 1747, 1762, 1832, 1832, 1981, 2094, 2201, 2356, 2617 \} $$

(these are not precise! have headroom for a few Hertz of deviations). Now you have to fit them into some harmonic families. Probably you will assume that the lowest frequencies belong to the fundamentals. After a bit of search, you may say that one possible organization of this chord is F major with these three notes on it:

  • F at 174 Hz
  • A at 220 Hz
  • C at 262 Hz

I hope you can see their upper harmonics and can individually discriminate which harmonic belong to which note. Also, note that, certain upper harmonics of different fundamentals can fall quite close in frequency.

You can continue this analysis for the remaning four chords. The code is below:

clc; clear all; close all;

[x,Fs,Nb] = wavread('C:\PathToFile\sample1.wav',[1, 45000]);
figure,plot(x);title('signal x[n] sampled at 44100 Hz, 16 bits');

y = reshape( x, 9000, 5 );
Y = fft(y,Fs);

figure,plot((1/9000)*abs(Y(:,1)).^2);
title('Periodogram of the first chord block |X_1[k]|^2|');

Note that I have found the (approximate) peak frequencies by simple visual inspection. This was partly justified by the basic type of the spectrum.

$\endgroup$
3
  • $\begingroup$ Perfect answer I actually have been given a "family" to choose from. Would you be able to show me the code used? Because as beautiful as the answer is I am not able to reproduce it. $\endgroup$ Commented Dec 12, 2018 at 0:04
  • $\begingroup$ it's good to know. Let me add the code. $\endgroup$
    – Fat32
    Commented Dec 12, 2018 at 0:05
  • $\begingroup$ Thank you again very much for spending your valuable time on this! $\endgroup$ Commented Dec 12, 2018 at 0:26
2
$\begingroup$

Unless your audio data consists of pure sinusoidal note sources, a raw FFT magnitude plot will give you all the harmonics or overtones of pitched musical sounds, as well as any fundamental pitch frequency spectrum (if any). So, with most common polyphonic music recordings, pitch detection/estimation, if possible, usually involves a significant amount of post-processing of any FFT results. Not just picking the largest magnitude peaks.

Either Cepstral processing (or computing the Cepstrum), or the Harmonic Product Spectrum algorithm, look for periodic sequences of overtones or harmonic peaks embedded in FFT spectra, which might indicate a source pitch candidate. (e.g. Look for embedded trains of some number of magnitude peaks at evenly spaced frequency multiples.) But, especially with polyphony and many common chord types, there will likely be some number of false positives that you will have to figure out how to discount or ignore.

Or, instead of using an FFT, you could also try looking at auto-correlation peaks (or other lag-based partial similarity measures), and see if these peaks correspond to the appropriate pitch range and degree of polyphony.

$\endgroup$

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.