Sum of samples values of an audio track

Help for Audacity on Windows.
Forum rules
ImageThis forum is for Audacity on Windows.
Please state which version of Windows you are using,
and the exact three-section version number of Audacity from "Help menu > About Audacity".


Audacity 1.2.x and 1.3.x are obsolete and no longer supported. If you still have those versions, please upgrade at https://www.audacityteam.org/download/.
The old forums for those versions are now closed, but you can still read the archives of the 1.2.x and 1.3.x forums.
Post Reply
KerimF
Posts: 110
Joined: Thu Jan 31, 2019 4:10 pm
Operating System: Windows 7

Sum of samples values of an audio track

Post by KerimF » Sun Mar 03, 2019 10:46 pm

Sorry if my question sounds somehow naïve.

Is there a plug-ins which gives, in a way, the algebraic sum of all samples of a ‘wav’ track?

The sum, in case of a normal audio track, is zero or close to it.
But if the values of the ‘wav’ samples were corrupted, this sum would likely be far from zero. In case the sum is a big number and exceeds a certain high limit, the plug-ins needs returning the value of the limit only.

Thank you.
Kerim

steve
Site Admin
Posts: 81651
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Sum of samples values of an audio track

Post by steve » Sun Mar 03, 2019 11:38 pm

So long as you're not dealing with a very long track, and the track is mono, running this code in the Nyquist Prompt effect will give you the sum:

Code: Select all

;type analyze
;version 4

(let ((t1 (/ (1- len) *sound-srate*))
      (integral (mult (/ *sound-srate* len) (integrate *track*))))
  (print (abs-env (sref integral t1))))

9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

KerimF
Posts: 110
Joined: Thu Jan 31, 2019 4:10 pm
Operating System: Windows 7

Re: Sum of samples values of an audio track

Post by KerimF » Mon Mar 04, 2019 7:46 am

Thank you, Steve. You are fast as usual :)

For instance, the word 'integral' on your code reminded me the low pass filter. Even with 0.1 Hz bandwidth (lowest limit), the filter output gives a good visual sign about the average DC of the track.

Edit1:
I run 2.3.0 on windows 7. I tested your code on a short mono track ("M_RoadToHell_ChrisRea1.wav", I uploaded earlier). I got:

Code: Select all

error: division by zero
Function: #<Subr-/: #a2f1780>
Arguments:
  44100
  0
Function: #<FSubr-LET: #a2ec230>
Arguments:
  ((T1 (/ (1- LEN) *SOUND-SRATE*)) (INTEGRAL (MULT (/ *SOUND-SRATE* LEN) (INTEGRATE *TRACK*))))
  (PRINT (ABS-ENV (SREF INTEGRAL T1)))
1>
Edit2:
Very sorry, I forgot to select the track.
Now I got the value 0.000110
Thank you again.

Edit3:
Actually, it is 0.000110462 (Debug Output).

steve
Site Admin
Posts: 81651
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Sum of samples values of an audio track

Post by steve » Mon Mar 04, 2019 8:34 am

KerimF wrote:
Mon Mar 04, 2019 7:46 am
Very sorry, I forgot to select the track.
:)
That could have been avoided by error checking. for example:

Code: Select all

;type analyze
;version 4

(cond
  ((= len 0)
    "Error.\nSelection Required")
  ((arrayp *track*)
    "Error.\nStereo tracks are not supported.")
  (t
    (let ((t1 (/ (1- len) *sound-srate*))
          (integral (mult (/ *sound-srate* len)
                          (integrate *track*))))
      (print (abs-env (sref integral t1))))))

KerimF wrote:
Mon Mar 04, 2019 7:46 am
For instance, the word 'integral' on your code reminded me the low pass filter. Even with 0.1 Hz bandwidth (lowest limit), the filter output gives a good visual sign about the average DC of the track.
Another way you could do that would be to duplicate the track, then use the Normalize effect to "Remove DC offset" (only), then invert the duplicate track and mix the two tracks (Tracks menu > Mix > Mix and Render). The result will be the DC offset. This could also be automated with a Macro (https://manual.audacityteam.org/man/macros.html), or a Nyquist-Macro (https://manual.audacityteam.org/man/nyquist_macros.html).
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

KerimF
Posts: 110
Joined: Thu Jan 31, 2019 4:10 pm
Operating System: Windows 7

Re: Sum of samples values of an audio track

Post by KerimF » Mon Mar 04, 2019 8:43 am

steve wrote:
Mon Mar 04, 2019 8:34 am
[Another way you could do that would be to duplicate the track, then use the Normalize effect to "Remove DC offset" (only), then invert the duplicate track and mix the two tracks (Tracks menu > Mix > Mix and Render). The result will be the DC offset. This could also be automated with a Macro (https://manual.audacityteam.org/man/macros.html), or a Nyquist-Macro (https://manual.audacityteam.org/man/nyquist_macros.html).
Another good solution, thank you.

(Here it is 10:43 AM, so I thought you were in bed now on the other side of the planet :) )

steve
Site Admin
Posts: 81651
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Sum of samples values of an audio track

Post by steve » Mon Mar 04, 2019 10:29 am

Just for kicks, here's a version that can handle stereo tracks. If you wanted to, you could add "headers" to convert this into an installable plug-in (https://wiki.audacityteam.org/wiki/Nyqu ... nce#header).

Code: Select all

;type analyze
;version 4

(defun get-offset(sig)
  (let ((t1 (/ (1- len) *sound-srate*))
        (integral (mult (/ *sound-srate* len)
                        (integrate sig))))
    (abs-env (sref integral t1))))


(setf *float-format* "%.4f")  ;format with 4 decimal places

(if (= len 0)
    "Error.\nSelection Required"
    (if (arrayp *track*)
        (format nil "Left offset: ~a~%Right offset: ~a"
                (get-offset (aref *track* 0))
                (get-offset (aref *track* 1)))
        (format nil "Offset: ~a" (get-offset *track*))))

9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

KerimF
Posts: 110
Joined: Thu Jan 31, 2019 4:10 pm
Operating System: Windows 7

Re: Sum of samples values of an audio track

Post by KerimF » Mon Mar 04, 2019 9:11 pm

Thank you, Steve, for the extra work. I saved it and I will install it as a new plug-in.

Although this sum/average function may help, as I mentioned earlier, in detecting corrupted samples in rare cases, it is useful in the case (about which we will talk later if you like) when a different signature is inserted in every copy of an audio file but without affecting in any way the audio quality and its originality... also its DC zero average.

Edited
Last edited by KerimF on Mon Mar 04, 2019 11:37 pm, edited 2 times in total.

KerimF
Posts: 110
Joined: Thu Jan 31, 2019 4:10 pm
Operating System: Windows 7

Re: Sum of samples values of an audio track

Post by KerimF » Mon Mar 04, 2019 11:32 pm

I think the following headers are enough to let your codes (for mono and stereo) be installable plug-ins:

Code: Select all

;nyquist plug-in
;version 4
;type analyze
;name "SumMono"
;author "Steve_AudacityTeam"
and

Code: Select all

;nyquist plug-in
;version 4
;type analyze
;name "SumStereo"
;author "Steve_AudacityTeam"
Edit:
Corrected
Last edited by KerimF on Mon Mar 04, 2019 11:46 pm, edited 1 time in total.

steve
Site Admin
Posts: 81651
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Sum of samples values of an audio track

Post by steve » Mon Mar 04, 2019 11:36 pm

It should probably be ";type analyze", given that it is analyzing the audio.
";type process" is intended for plug-ins that return a modified (processed) version of the selected audio.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

KerimF
Posts: 110
Joined: Thu Jan 31, 2019 4:10 pm
Operating System: Windows 7

Re: Sum of samples values of an audio track

Post by KerimF » Mon Mar 04, 2019 11:40 pm

steve wrote:
Mon Mar 04, 2019 11:36 pm
It should probably be ";type analyze", given that it is analyzing the audio.
";type process" is intended for plug-ins that return a modified (processed) version of the selected audio.
That is why I asked before installing them :) Thank you.

Post Reply