Page 1 of 1

Exporting amplitude/time data for each sample

Posted: Wed Sep 30, 2009 4:43 pm
by tomharding
Hi,

I am doing a math project for which I am trying to make sine models for some recorded sine waves. In order to do this, I need to convert audio e.g. wav into a table with time/sample and amplitude.

I have searched everywhere for a way to do this in Audacity or otherwise, but it has turned up nothing really except attempting to write my own program. Obviously this would take a long time as I have almost no programming experience.

Does anyone have any advice?

Thanks,

Tom

Re: Exporting amplitude/time data for each sample

Posted: Wed Sep 30, 2009 9:14 pm
by steve
For a MONO track only.

Select a bit of the track, then from the Effects menu choose the "Nyquist Prompt"

Enter this code into the box

Code: Select all

;; Read 1000 samples into array
(setf samplearray (make-array (snd-length s 1000)))

(do 
   ((n 0 (setq n (1+ n))) 
   (nextsample (snd-fetch s) (setq nextsample (snd-fetch s))))

;; Exit when we run out of samples (nextsample is nil) and return the array "samplearray"
   ((eql n 1000) samplearray)

;; Start the execution part of the "do" loop
   (setf (aref samplearray n) nextsample)
   (princ (format nil "~a = ~a ~%" n (aref samplearray n))))
s
Then press the "Debug" button.

It will display the values of the first 1000 samples in the selection.
The "time" value depends on the sampling rate of your audio. If the track has a sample rate of 44100Hz, that means that there are 44100 samples each second, so each sample will follow the previous one after 1/44100 second (0.000022676 seconds).

Note that this destroys the first 1000 samples of the selection.
If there are less than 1000 samples, results will be given for all available samples, and then an error is generated.

Re: Exporting amplitude/time data for each sample

Posted: Wed Sep 30, 2009 9:32 pm
by steve
If you need a version that does not destroy the samples:

Code: Select all

(setf s-in (snd-copy s))

;; Read 1000 samples into array
(setf samplearray (make-array (snd-length s-in 1000)))
(do 
   ((n 0 (setq n (1+ n))) 
   (nextsample (snd-fetch s-in) (setq nextsample (snd-fetch s-in))))
;; Exit when we run out of samples (nextsample is nil) and return the array "samplearray"
   ((eql n 1000) samplearray)

;; Start the execution part of the "do" loop
   (setf (aref samplearray n) nextsample)
   (princ (format nil "~a = ~a ~%" n (aref samplearray n))))
s