From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Pander Newsgroups: gmane.comp.audio.sox.devel Subject: Re: Follow up on Average power spectrum path Date: Mon, 28 Dec 2015 18:38:46 +0100 Message-ID: <568173A6.7060701@users.sourceforge.net> References: <5672C4E3.9090605@users.sourceforge.net> <20151219111235.GA24760@dcvr.yhbt.net> Reply-To: sox-devel@lists.sourceforge.net NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1451324363 9412 80.91.229.3 (28 Dec 2015 17:39:23 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 28 Dec 2015 17:39:23 +0000 (UTC) To: sox-devel@lists.sourceforge.net Original-X-From: sox-devel-bounces@lists.sourceforge.net Mon Dec 28 18:39:13 2015 Return-path: Envelope-to: gcasd-sox-devel@m.gmane.org X-ACL-Warn: X-Enigmail-Draft-Status: N1110 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 In-Reply-To: <20151219111235.GA24760@dcvr.yhbt.net> X-Spam-Score: -0.8 (/) X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -0.8 AWL AWL: Adjusted score from AWL reputation of From: address X-Headers-End: 1aDbkz-0003nZ-D7 X-BeenThere: sox-devel@lists.sourceforge.net X-Mailman-Version: 2.1.9 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: sox-devel-bounces@lists.sourceforge.net Xref: news.gmane.org gmane.comp.audio.sox.devel:460 Archived-At: Received: from lists.sourceforge.net ([216.34.181.88]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1aDblJ-0004iF-40 for gcasd-sox-devel@m.gmane.org; Mon, 28 Dec 2015 18:39:13 +0100 Received: from localhost ([127.0.0.1] helo=sfs-ml-4.v29.ch3.sourceforge.com) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1aDbl1-00033H-KU; Mon, 28 Dec 2015 17:38:55 +0000 Received: from sog-mx-4.v43.ch3.sourceforge.com ([172.29.43.194] helo=mx.sourceforge.net) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1aDbl0-00033C-Ts for sox-devel@lists.sourceforge.net; Mon, 28 Dec 2015 17:38:54 +0000 Received: from nodachi.mjopr.nl ([83.96.169.215]) by sog-mx-4.v43.ch3.sourceforge.com with esmtps (TLSv1:AES256-SHA:256) (Exim 4.76) id 1aDbkz-0003nZ-D7 for sox-devel@lists.sourceforge.net; Mon, 28 Dec 2015 17:38:54 +0000 Received: from [192.168.1.12] (ip235-130-212-87.adsl2.static.versatel.nl [87.212.130.235]) (using TLSv1 with cipher ECDHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) (Authenticated sender: sander) by nodachi.mjopr.nl (Postfix) with ESMTPSA id CE3BC1FC1B for ; Mon, 28 Dec 2015 18:38:46 +0100 (CET) On 12/19/2015 12:12 PM, Eric Wong wrote: > Pander wrote: >> Because of a question on the users list, I would like to ask how to move >> forward the following patch and documentation? > > I guess the real sox developers are still busy... > I suppose I'll start publishing things I've reviewed/tested > and push things I've reviewed to branches my server (see below)[1] > so it'll hopefully be easier for them when they return. > > I'm not really qualified to review the maths parts of this; > as I am only a simple Unix/C plumber and I already commented > some last year on this patch... > > I wonder if "-a" should imply "-freq", since it's "-a" > is a no-op without "-freq". It is only allowed/used after -freq in this context. After gaining some more experience with sox this year, I have learned that calculating the average power spectrum is not straightforward. I have also posted about this to one of the sox mailing lists where I illustrated the problem with some graphs. Only when the last sample is filled completely, an average can be calculated over all samples. Usually, this is not the case and should the average be calculated over over all samples except the last one. The safest way to calculate the average, is to always omit the last sample. However, it is more correct to include the last sample, but only when can be detected that the last sample is filled exactly as all the other samples are. If it is only partially filled, it (highly probably) will distort the average too much. Since I only work with a lot of samples, for me you can always omit the last one. But if someone could let you know how to detect that the last samples is used to the full and can be included, please do so. Of course, when omitting the last sample, divide by the number of samples - 1. Either way, how this is implemented, it should be documented clearly when the last sample is omitted. > > And is this option is intended to be the average of each hunk in the > flow and not for the entire file? In other words, it outputs a LOT of > lines with 2 floating point numbers in them when I run: > "sox foo.flac -n stat -freq -a" on an arbitrary file. > > Most of the normal "stat" output is printed in the sox_stat_stop > function instead of sox_stat_flow; the lone exception being '-d' > which seems mainly for troubleshooting. > > Also, did you intend fft_average to be extended for non-boolean > purposes? Initially not, but see above. It could be desirable to offer different options: nothing: do as now -a average over all samples -ao average over all samples but omit last sample -as average over all samples but omit last sample only when needed (often needed because the last sample is partially filled) > > I don't mind using "int" as a bool for other pre-C99 projects, but the > "== 1" checks in conditionals looked a bit odd to me and sox already > defines a sox_bool type, so I figure squashing in the following would > improve readability: > > diff --git a/src/stat.c b/src/stat.c > index 63d1741..89bed55 100644 > --- a/src/stat.c > +++ b/src/stat.c > @@ -34,7 +34,7 @@ typedef struct { > float *re_out; > unsigned long fft_size; > unsigned long fft_offset; > - int fft_average; > + sox_bool fft_average; > } priv_t; > > > @@ -71,7 +71,7 @@ static int sox_stat_getopts(sox_effect_t * effp, int argc, char **argv) > else if (!(strcmp(*argv, "-d"))) > stat->volume = 2; > else if (!(strcmp(*argv, "-a"))) > - stat->fft_average = 1; > + stat->fft_average = sox_true; > else { > lsx_fail("Summary effect: unknown option"); > return SOX_EOF; > @@ -103,7 +103,7 @@ static int sox_stat_start(sox_effect_t * effp) > stat->bin[i] = 0; > > stat->fft_size = 4096; > - stat->fft_average = 0; > + stat->fft_average = sox_false; > stat->re_in = stat->re_out = NULL; > > if (stat->fft) { > @@ -142,7 +142,8 @@ static int sox_stat_flow(sox_effect_t * effp, const sox_sample_t *ibuf, sox_samp > unsigned samples = 0; > float ffa = 0.0; > unsigned i; > - if (stat->fft_average == 1) { > + > + if (stat->fft_average) { > samples = (stat->fft_size / 2); > ffa = effp->in_signal.rate / samples; > re_average = lsx_malloc(sizeof(float) * (int)samples); > @@ -159,7 +160,7 @@ static int sox_stat_flow(sox_effect_t * effp, const sox_sample_t *ibuf, sox_samp > > if (stat->fft_offset >= stat->fft_size) { > stat->fft_offset = 0; > - if (stat->fft_average == 1) { > + if (stat->fft_average) { > lsx_power_spectrum_f((int)samples, stat->re_in, stat->re_out); > for (i = 0; i < samples / 2; i++) /* FIXME: should be <= samples / 2 */ > re_average[i] += stat->re_out[i]; > @@ -169,7 +170,7 @@ static int sox_stat_flow(sox_effect_t * effp, const sox_sample_t *ibuf, sox_samp > } > > } > - if (stat->fft_average == 1) { > + if (stat->fft_average) { > for (i = 0; i < samples / 2; i++) /* FIXME: should be <= samples / 2 */ > fprintf(stderr, " %f %f\n", ffa * i, re_average[i] / len); > } > > --- > [1] you can add my repo to your existing sox repository: > git remote add 80x24 git://80x24.org/sox.git > git fetch 80x24 > (or a web viewer if you prefer: http://bogomips.org/sox.git ) > These are work-in-progress dev branches, so I will squash > and rebase branches as needed with forced pushes. > > ------------------------------------------------------------------------------ > _______________________________________________ > SoX-devel mailing list > SoX-devel@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/sox-devel > ------------------------------------------------------------------------------