1

Background I'm trying to validate audio data received over RTP for its accuracy when compared to original source. In my system the audio is played by embedded platform devices and sent out on network for other devices to capture and play. it's specs vary based on mono, stereo or surround audio but sample rate and bit specs are as per following.

I'm using .wav file for now which contains Sine wave of spec, 44.1 kHz, 440 frequency, 1-channel, 16-bit PCM data. I'm using Sine wave so that it is clean and easy to analyze.

using following Python code I could verify that both the files are same since the alignment.distance it gives is 0.0

import librosa
import librosa.display
import matplotlib.pyplot as plt
from numpy.linalg import norm
from dtw import dtw

# Loading audio files
y1, sr1 = librosa.load("./test-audio-files/1kHz_44100Hz_16bit_05sec.wav")
y2, sr2 = librosa.load("./test-audio-files/received.wav")

# Computing MFCC values
mfcc1 = librosa.feature.mfcc(y1, sr1)
mfcc2 = librosa.feature.mfcc(y2, sr2)

alignment = dtw(mfcc1.T, mfcc2.T)
print("The normalized distance between the two : ", alignment.distance)  # 0 for similar audios

Query what I'm wondering is, how to validate the accuracy of the sine wave? The above solution would work as far as I make sure my source file is perfect and accurate. If the source file has any problem then also the above solution would claim they match. I'm playing with .wav file for now but it can be any file like mp3, mp4...

The following packet loss is what I am trying to detect:

enter image description here

Reference Downloaded the .wav file from https://www.mediacollege.com/downloads/

user2669989
  • 308
  • 1
  • 2
  • 15
  • You want to measure the time-shift? Or are you looking for distortions, corruptions etc? – Jon Nordby Nov 05 '21 at 04:41
  • my intention to find if the sine wave is a sine wave or nor. due to network loss or software bug it might loss a packet and would make imperfect sine wave. See my Edit1 with the image. – user2669989 Nov 05 '21 at 14:03
  • 2
    As far as adjusting to packet loss have you considered `box car averaging` where you take `x` points and average them? Then starting with the next point, take `x` more and continue in this fashion. And as far as determining the frequency there is always the `Nyquist formula`. But these more or less reproduce the pattern rather than validate it. – WJS Nov 05 '21 at 14:18
  • 1
    @WJS, after reading your comment, I searched and wonder if I could use `scipy.fft.fftfreq` to get `Discrete Fourier Transform`. Then sum the `Min` and `Max`, the result should be `-1` for the sine wave to be complete and valid. I wonder how much I can rely on this? provided the input wav file would be bad or valid. – user2669989 Nov 08 '21 at 23:31

2 Answers2

0

If you know the frequency of the test sine wave, you could mathematically generate a perfect sinewave, align the peak, and compare offsets and amplitude. If you don't know what type of signal you are testing, but still wanted to test if it was a sine wave, you could try a regression to fit a sine wave to it. Note: The higher frequencies may appear(confound) as packet loss, but may be in fact a source sampling limitation i.e: a 70KHz signal sampled at 44 KHz.

betacrash
  • 59
  • 7
  • I would know the sine wave frequency, frame rate, duration and amplitude. I don't really know how to do that in python. Is there any reference which explains this better. Though I've one more theory per my reply to WJS's comment above. – user2669989 Nov 08 '21 at 23:22
0

If you want to ensure that the original sine wave is accurate an approach could be to generate the sine wave directly on python and then use scipy's scipy.io.wavfile.write to generate the .wav file.

from scipy.io.wavfile import write

samplerate = 44100; fs = 100
t = np.linspace(0., 1., samplerate)
amplitude = np.iinfo(np.int16).max
data = amplitude * np.sin(2. * np.pi * fs * t)
write("example.wav", samplerate, data.astype(np.int16))
Arjuna Deva
  • 275
  • 4
  • 17