# Sum of samples values of an audio track

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

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:

``````;type analyze
;version 4

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

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:

``````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).

That could have been avoided by error checking. for example:

``````;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))))))
``````

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 (Macros - Audacity Manual), or a Nyquist-Macro (Nyquist-Macros - Audacity Manual).

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 )

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/Nyquist_Plug-ins_Reference#header).

``````;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*))))
``````

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

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

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

and

``````;nyquist plug-in
;version 4
;type analyze
;name "SumStereo"
;author "Steve_AudacityTeam"
``````

Edit:
Corrected

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.