Saturday, September 30, 2023
Derniers articles
Accueil > Unclassified > Spectral analysis of a sound
Unclassified

# Spectral analysis of a sound

## 1. Introduction

We are trying to perform the spectral analysis of a sound emitted by a musical instrument, at a constant height. The sound is recorded in a WAV file. We ask the instrumentalist to play a note while maintaining the sound then we carry out the recording after the attack, for a duration of about 1 second, so as to obtain a constant sound, in pitch and intensity.

## 2. Processing with Mathematica

The WAV file can be played as a sound object:

```
file = "clarinette_la3.wav";
```
`son = Import[file]`

The bottom curve is a temporal representation of sound, which in this case shows an almost constant amplitude. The top curve is a time-frequency analysis that shows a constant spectrum over the duration of the sound. We set the sampling frequency to the value indicated above then we recover the signal in the form of a list of values:

```
fe = 22050
signal = Import[file,"Data"][[1]];
```

The following function plots the amplitude versus time for part of the signal. The time unit of the start and duration arguments is the sampling period.

```tracer[signal_,debut_,duree_]:=Module[{onde},
onde = Table[{k/fe,signal[[k+debut]]},{k,duree}];
ListPlot[onde,Joined->True,AxesLabel->{"t (s)","s"}]
]
```
`tracer[signal,100,300]`

The spectrum is obtained by means of the discrete Fourier transform. The following function calculates and plots the spectrum on the part of the chosen signal. The greatest frequency of the spectrum is equal to half of the sampling frequency, here 10 kHz. The resolution of the spectrum is equal to the inverse of the duration of the sample used.

```spectre[signal_,debut_,duree_,plotrange_]:=Module[{echantillon,tfd,sp},
echantillon = Take[signal,{debut,debut+duree}];
tfd = Fourier[echantillon,FourierParameters->{-1,-1}];
sp = Table[{(k-1)/duree*fe,Abs[tfd[[k]]]*duree/fe},{k,duree/2}];
ListPlot[sp,Filling->Axis,AxesLabel->{"f(Hz)","|Cn|"},PlotRange->plotrange]
]
```
`spectre[signal,100,3000,{{0,5000},{0,0.002}}]`

The spectrum obtained is practically discrete, with lines whose frequencies are multiple of a fundamental frequency, here 440 Hz. The spectrum obtained therefore provides the Fourier coefficients of the signal, considered as a periodic function.

In the previous example, the chosen sample has a duration of 3000 points. Let’s see an analysis performed on a sample of the same duration but located towards the end of the sound:

`spectre[signal,20000,3000,{{0,5000},{0,0.002}}]`

The first 5 harmonics have hardly changed.

Let’s see how to use the DFT to obtain the fundamental frequency of the sound, with a precision of 1 Hz. For that, it is necessary to make the calculation over a duration of 1 second, which does not pose a problem in the present case since the spectrum seems constant over this period:

`spectre[signal,1,22000,{{430,450},{0,0.02}}]`

The fundamental frequency is 441.0 Hz plus or minus 0.5 Hz.

## 3. Examples

### 3.a. Clarinet

```
signal = Import["clarinette_re1.wav","Data"][[1]];
```
`spectre[signal,1000,3000,{{0,4000},{0,0.001}}]`

In this low register, the spectrum is very rich in harmonics. The third harmonic (corresponding to an octave and a fifth in relation to the fundamental) is here more intense than the fundamental. The 5th order harmonic (2 octaves plus a third) is the most intense.

Let’s see the D one octave higher:

```signal = Import["clarinette_re2.wav","Data"][[1]];
```
`spectre[signal,1000,3000,{{0,4000},{0,0.001}}]`

The third and fifth harmonics are present but this time weaker than the fundamental. The octave (rank 2) is almost zero.

Let’s see the A straight above:

```signal = Import["clarinette_la3.wav","Data"][[1]];
```
`spectre[signal,1000,3000,{{0,4000},{0,0.001}}]`

The 3rd order harmonic is very intense, while the 5th order is very weak. One octave higher:

```signal = Import["clarinette_la4.wav","Data"][[1]];
```
`spectre[signal,1000,3000,{{0,6000},{0,0.001}}]`

The spectrum here is completely different, with rank 2 and rank 3 almost at the same level.

### 3.b. Recorder

```signal = Import["fluteBec_la4.wav","Data"][[1]];
```
`spectre[signal,1000,3000,{{0,6000},{0,0.001}}]`

The third and fifth harmonics are visible, but are very weak compared to the fundamental.