asha ganapathy
asha ganapathy

Reputation: 113

Understanding an wav file exported from a daw

I have generated a tone from Audacity at 440Hz with Amplitude as 1 for 1 sec like this: enter image description here

I understand that this is going to create 440 peaks in 1 sec with Amplitude as 1. Here i see that its a 32 bit file with 44100Hz is the sample rate which means there are 44100 samples per sec. The Amplitude is 1 which is as expected because that is what i chose.

enter image description here

What i dont understand is, what is the unit of this Amplitude? When right-clicked it shows linear(-1 to +1) There is an option to select dB it shows (0 to -60 to 0) which i dont understand how is this converted! enter image description here

now when i use this wav file in the python scipy to read the wav and get values of time and amplitude How to match or get the relation between what i generated vs what i see when i read a wav file? The peak is amplitude is 32767.987724003342 Frequency 439.99002267573695

enter image description here

The code i have used in python is

wavFileName ="440Hz.wav"
sample_rate, sample_data = wavfile.read(wavFileName)
print ("Sample Rate or Sampling Frequency is", sample_rate," Hz")
l_audio = len(sample_data.shape)
print ("Channels", l_audio,"Audio data shape",sample_data.shape,"l_audio",l_audio)
if l_audio == 2:
    sample_data = sample_data.sum(axis=1) / 2
N = sample_data.shape[0]
length = N / sample_rate
print ("Duration of audio wav file in secs", length,"Number of Samples chosen",sample_data.shape[0])
time =np.linspace(0, length, sample_data.shape[0])
sampling_interval=time[1]-time[0]

Upvotes: 0

Views: 123

Answers (1)

Scott Stensland
Scott Stensland

Reputation: 28285

notice in audacity when you created the one second of audio with aplitude choice of 1.0 right before saving file it says signed 16 bit integer so amplitude from -1 to +1 means the WAV file in PCM format stores your raw audio by varying signed integers from its max negative to its max positive which since 2^16 is 65536 then signed 16 bit int range is -32768 to 32767 in other words from -2^15 to ( +2^15 - 1 ) ... to better plot I suggest you choose a shorter time period much smaller than one second lets say 0.1 seconds ... once your OK with that then boost it back to using a full one second which is hard to visualize on a plot due to 44100 samples

import os
import scipy.io
import scipy.io.wavfile
import numpy as np
import matplotlib.pyplot as plt

myAudioFilename = '/home/olof/sine_wave_440_Hz.wav'
   
samplerate, audio_buffer = scipy.io.wavfile.read(myAudioFilename)

duration = len(audio_buffer)/samplerate

time = np.arange(0,duration,1/samplerate) #time vector


plt.plot(time,audio_buffer)
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.title(myAudioFilename)
plt.show()

here is 0.1 seconds of 440 Hz using signed 16 bit notice the Y axis of Amplitude range matches above mentioned min to max signed integer value range

Upvotes: 1

Related Questions