From patchwork Fri Sep 18 16:50:48 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Caleb Crome X-Patchwork-Id: 7219331 Return-Path: X-Original-To: patchwork-alsa-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 43CEFBEEC1 for ; Fri, 18 Sep 2015 16:51:28 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 442CF206A0 for ; Fri, 18 Sep 2015 16:51:27 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) by mail.kernel.org (Postfix) with ESMTP id 846E620671 for ; Fri, 18 Sep 2015 16:51:25 +0000 (UTC) Received: by alsa0.perex.cz (Postfix, from userid 1000) id B47AC265DEF; Fri, 18 Sep 2015 18:51:23 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,NO_DNS_FOR_FROM, RCVD_IN_DNSWL_LOW, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 Received: from alsa0.perex.cz (localhost [IPv6:::1]) by alsa0.perex.cz (Postfix) with ESMTP id 4EBC5265A03; Fri, 18 Sep 2015 18:51:16 +0200 (CEST) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa0.perex.cz (Postfix, from userid 1000) id ED192265D47; Fri, 18 Sep 2015 18:51:14 +0200 (CEST) Received: from mail-wi0-f174.google.com (mail-wi0-f174.google.com [209.85.212.174]) by alsa0.perex.cz (Postfix) with ESMTP id 1BCB4265703 for ; Fri, 18 Sep 2015 18:51:07 +0200 (CEST) Received: by wicfx3 with SMTP id fx3so38474421wic.0 for ; Fri, 18 Sep 2015 09:51:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=amPw7a/M6dW/BKIKr/q8QDudTMt3XX8lZy1dc8koS40=; b=j0WUvxjKk0L1e+EZpfINWLy8oLbUNHQGOQhLL8v8+Ud9AGY5pG9MsJWc1Xz/q2VwDM 8I7pGp/fXRNkvLWaXUVx9gOAc9L3zBiKRLCZg9V6TEz3Xd2w8C5wwNCg/CWSlaJhkKq5 fs/rUE1D7BzzV8KaF2mlpitQ1RCPzntGYX4CBisuSJriHnaetCZ14icwkQp6aGrGpLLA ssSz3p5k/DZ1fAG7jff89wS5rWdJRe4gjxKpDNJwvijOb0hDjCXQOGFBMz4yH24djhay fc5mUPAGneeKWKZa7EubDzrNS4PNIgLnkjmklT98i91TnUookJqMq7F9uVMM7+LnWe28 iEdA== X-Gm-Message-State: ALoCoQlh13r/iEqmpRbVMXpSetEZy73gh35kdvH9VCqkHFYZY4aw/SeITd8+4aSOz99g8B+ujuuA X-Received: by 10.180.8.132 with SMTP id r4mr41266394wia.70.1442595067556; Fri, 18 Sep 2015 09:51:07 -0700 (PDT) MIME-Version: 1.0 Received: by 10.27.86.103 with HTTP; Fri, 18 Sep 2015 09:50:48 -0700 (PDT) In-Reply-To: References: From: Caleb Crome Date: Fri, 18 Sep 2015 09:50:48 -0700 Message-ID: To: han.lu@intel.com Cc: bernard.gautier@intel.com, "alsa-devel@alsa-project.org" , tiwai@suse.com, liam.r.girdwood@intel.com Subject: Re: [alsa-devel] [PATCH BAT V1 4/7] BAT: Add signal generator X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: alsa-devel-bounces@alsa-project.org X-Virus-Scanned: ClamAV using ClamSMTP Hi Han, Liam, all, It looks like you have run into the old problem that the math library actually stinks at generating sine waves right :-) > + for (k = 0; k < length; k++) { > + for (c = 0; c < bat->channels; c++) { > + idx = k * bat->channels + c; > + out[idx] = sinf(val[c] * i) * factor; > + } > + i++; > + if (i == bat->rate) > + i = 0; /* Restart from 0 after one sine wave period */ > + } If I'm reading this right, you're resetting the argument to the sinf function. This can generate unwanted distortion in the sine wave if the frequency isn't an exact fraction of the sample rate. Here's a little library (along with a test program) I just whipped up using a phasor as the sine wave generator. The test program runs it for a simulated 3 days, then prints a python script to stdout that you can then run and plot to see the generated wave. This will work for any frequency sine wave, and as far as I know, it's the best way to generate sine waves. (Not for calculating the sin of a value though!) Here is the generator and test program. Sorry If this isn't the right way to submit a file/patch, but I have no idea how to do it properly :-/ BTW, this isn't actually integrated into the bat program, it's just the little library for generating the sine waves. Author: Caleb Crome Date: Fri Sep 18 09:45:43 2015 -0700 added sin_generator +float sin_generator_next_sample(struct sin_generator *sg); +void sin_generator_vfill(struct sin_generator *sg, float *buf, int n); diff --git a/bat/sin_generator.c b/bat/sin_generator.c new file mode 100644 index 0000000..e67f376 --- /dev/null +++ b/bat/sin_generator.c @@ -0,0 +1,120 @@ +#include "math.h" +#include "sin_generator.h" +/* + * Copyright (C) 2015 Caleb Crome + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +/* + * This is a general purpose sine wave generator that will stay stable + * for a long time, and with a little renormalization, could stay stay + * stable indefinitely + */ + + +/* Initialize the sine wave generator. + * sin_generator: gets initialized by this call. + * frequency: the frequency for the sine wave. must be < 0.5*sample_rate + * sample_rate: the the sample rate... + * returns 0 on success, -1 on error. + */ +int sin_generator_init(struct sin_generator *sg, float magnitude, float frequency, float sample_rate) +{ + // angular frequency: cycles/sec / (samp/sec) * rad/cycle = rad/samp + float w = frequency/sample_rate*2*M_PI; + if (frequency >= sample_rate/2) + return -1; + sg->phasor_real = cos(w); + sg->phasor_imag = sin(w); + sg->magnitude = magnitude; + sg->state_real = 0.0; + sg->state_imag = magnitude; + sg->frequency = frequency; + sg->sample_rate = sample_rate; + return 0; +} + +/* + * Generates the next sample in the sine wave. + * should be much faster than calling a sin function + * if it's inlined and optimized. + * + * returns the next value. no possibility of error. +*/ +float sin_generator_next_sample(struct sin_generator *sg) +{ + // get shorthand to pointers + const double pr = sg->phasor_real; + const double pi = sg->phasor_imag; + const double sr = sg->state_real; + const double si = sg->state_imag; + // step the phasor -- complex multiply + sg->state_real = sr * pr - si * pi; + sg->state_imag = sr * pi + pr * si; + return sr; // return the input value so sine wave starts at + // exactly 0.0 +} +/* fills a vector with a sine wave */ +void sin_generator_vfill(struct sin_generator *sg, float *buf, int n) +{ + int i; + for (i = 0; i < n; i++) { + *buf++ = sin_generator_next_sample(sg); + } +} + +#ifdef TEST_PHASOR +#define TESTSIZE (48000/1000) +// compile with this command to create executable that will +// auto-generate a self plotting plot :-) + +// gcc -g sin_generator.c -o sin_generator -lm -DTEST_PHASOR + +#include + +void dumpbuffer(const char *name, float *buffer, int n) +{ + int i; + printf("%s = [\n", name); + for (i = 0; i < n; i++) { + printf(" %f,\n", buffer[i]); + } + printf("]\n"); + printf("plot(%s)\n", name); +} + +int main(int argc, char *argv[]) +{ + float buffer[TESTSIZE]; + long int i; + struct sin_generator sg; + + // test the first few. + sin_generator_init (&sg, 3.0, 1000, 48000); + sin_generator_vfill(&sg, buffer, TESTSIZE); + printf("from pylab import *\n"); + dumpbuffer("s", buffer, TESTSIZE); + + // now wind the phasor for 3 days worth of samples to see + // if magnitude drifts. + for (i = 0; i < (3L*24*60*60*48000); i++) + sin_generator_next_sample(&sg); + + sin_generator_vfill(&sg, buffer, TESTSIZE); + // and let's see what that one looks like + dumpbuffer("o", buffer, TESTSIZE); + printf("show()\n"); + + return 0; +} +#endif diff --git a/bat/sin_generator.h b/bat/sin_generator.h new file mode 100644 index 0000000..2ab7561 --- /dev/null +++ b/bat/sin_generator.h @@ -0,0 +1,23 @@ +/* + * Here's a generic sine wave generator that will work indefinitely for any frequency. + * + * Note: the state & phasor are stored as doubles (and updated as + * doubles) because after a million samples the magnitude drifts a + * bit. If we really need floats, it can be done with periodic + * renormalization of the state_real+state_imag magnitudes. I was + */ + +struct sin_generator; +struct sin_generator { + double state_real; + double state_imag; + double phasor_real; + double phasor_imag; + float frequency; + float sample_rate; + float magnitude; +}; + +int sin_generator_init(struct sin_generator *sg, float magnitude, float frequency, float sample_rate);