5

I've also asked this here on the Sound Design forum, but the question is heavy computer science/math so it might actually belong on this forum:

So I'm able to successfully find all the information about a WAV file except the amplitude and the frequency (hertz) of the big sin function by reading the binaries in the file (which are unfortunately exactly what I'm looking for). Just to verify what I'm talking about, the file generates one wave only with the equation:

F(s) = A * sin(T * s)

Where s is the current sample, A is the amplitude and T is the period. Now the equation for the T (period) is:

T = (2π * Hz) /(α * ω)

Where Hz is frequency in Hertz, α is Samples per second, and ω is the amount of channels.

Now I know that to solve for amplitude, I could simply find the value of F(s) where

s = (π/2)/T

Because then the value of the sine function would be 1, and the final value would be equivalent to A. The problem is that to divide by T, I have to know the Hertz (or Hz).

Is there any way that I can read a WAV file to discover the Hertz from the data, assuming the file only contains a single wave.

Community
  • 1
  • 1
Nas Kas
  • 53
  • 4
  • Method 1 (bad): Read the .wav data, i.e. the floats that represent the sound in time, then get the maximum value of all the time values to discover the amplitude, then check for a reoccuring sequence (as closely as possible) to find the frequency. Method two (better suited, but math-heavy): Use the discrete fourier transformation to transform your signal into the domain of frequency. A sine wave of the form `x(t) = A*sin(omega * t)` will be DFT transformed to a single dirac impulse located at *omega* and having amplitude `A`. This method is more suited as this is a signal processing problem. – Maximilian Gerhardt Feb 13 '16 at 23:13
  • @MaximilianGerhardt: Actually, if you know _a priori_ that the input is a sine wave, the first method is actually more accurate. It can find the time period with subsample accuracy. – MSalters Feb 13 '16 at 23:34
  • There's an answer here. It's not trivial. https://math.stackexchange.com/questions/36725/how-to-fit-a-curve-to-a-sinusoidal-wave – duffymo Feb 14 '16 at 14:28

2 Answers2

1

Just to get some terms clarified, the property you're looking for is frequency, and the unit of frequency is Hertz (once per second). By convention, the typical A note has a frequency of 440 Hz.

You got the function wrong, actually. That sine wave in reality has the form F(s) = A * sin(2*pi*s/T + c) - you don't know when it started so you get a constant c in there. Also, you need to divide by T, not multiply.

Getting the amplitude is actually fairly easy. That sine wave has a series op peaks and valleys. Find each peak (higher than both neighbors) and each valley (lower), calculate the average peak and average valley, and the amplitude is TWICE the difference between the two. Pretty easy. The period T can be estimated by counting the average distance from peak to peak, and from valley to valley.

There's one bit where you need to be careful. If there is a slight bit of noise, you may get a slight dent near a peak. Instead of 14 17 18 17 14 you may get 14 17 16 17 14. That 16 isn't a valley. Once you've got a good estimate for the real peaks and valleys, throw out all the distorted peaks.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Assuming my equation for frequency is correct (and yes by frequency I mean the tone/pitch measured in Hertz), and also say I'm fairly new to dealing with audio files (as of today), and say trying to learn the Fourier Transform wasn't super easy. How would you write a definitive equation for calculating the Tone/Pitch/Frequency of a wave in hertz, knowing that the only data you have is the output of the F(s) function (and the samples per sec, and channels)? – Nas Kas Feb 14 '16 at 04:13
  • @NasKas: I wouldn't write a "definitive equation". I would use the procedure in my answer. – MSalters Feb 14 '16 at 13:02
0

The question isn't "what frequency?". If your function is anything other than a simple trig function it'll be a combination of frequencies, each with their own amplitude.

The correct approach is digital signal processing using finite Fourier transform. You've got a lot of digging to do.

If you only want to assume a single trig function, you have just 2 (amplitude and frequency) or 3 degrees of freedom (amplitude, frequency, and phase angle) and N time points in the file. That means least squares fitting assuming a sine or cosine function.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Actually, only a repeating signal can be described by the Fourier Transform as a combination of frequencies. As for Least Squares, your link explains why it's not applicable here: `sin(s/T)` is about as non-linear as it gets. – MSalters Feb 14 '16 at 13:05
  • Fourier transforms are more general than that. If I model a singe wave form over a single period there's nothing repeating from the point of view of my problem. I can model a single Dirac delta or a square pulse. I know the difference between linear and non-linear least squares. A fit can still be done. Could be an incorrect link. – duffymo Feb 14 '16 at 13:20
  • If you model a single wave form over a single period, and take the Fourier Transform over **exactly** that period, you already knew the period and the FT would be a bit pointless. If you have a single wave (one period, the multiplication of a sine wave and a square pulse) and still take the FT from minus infinity to plus infinity, you get the convolution of that sine wave (dirac) with the `sinc` from the square pulse. – MSalters Feb 15 '16 at 08:04
  • Look at that - Stephen Wolfram doesn't think Fourier transforms of a square wave over a single period is pointless: https://www.google.com/search?q=fourier+transform+of+a+square+wave&espv=2&biw=1680&bih=921&tbm=isch&tbo=u&source=univ&sa=X&ved=0ahUKEwib_uynxPnKAhVQySYKHff0BSUQsAQIJw#imgrc=_ – duffymo Feb 15 '16 at 10:15
  • Sorry, it appears that your understanding of Fourier transforms and FFT is flawed. – duffymo Feb 15 '16 at 12:18