Android Open Source - audio-analyzer-for-android Double Sine Gen






From Project

Back to project page audio-analyzer-for-android.

License

The source code is released under:

Apache License

If you think the Android project audio-analyzer-for-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/* Copyright 2011 Google Inc.
 */*from   ww w.  j  av a  2  s  . co m*/
 *Licensed under the Apache License, Version 2.0 (the "License");
 *you may not use this file except in compliance with the License.
 *You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *Unless required by applicable law or agreed to in writing, software
 *distributed under the License is distributed on an "AS IS" BASIS,
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *See the License for the specific language governing permissions and
 *limitations under the License.
 *
 * @author Stephen Uhler
 */

package com.google.corp.productivity.specialprojects.android.samples.fft;

/**
 * Recursive sine wave generator
 * - compute parameters using double()
 *
 * y[n] = 2cos(w)y[n-1] - y[n-2]
 * w = 2 pi f / fs
 */

public class DoubleSineGen {
  private double fs;   // sampling frequency
  private double k;  // recursion constant
  private double n0, n1;  // first (next) 2 samples

  /**
   * Create a sine wave generator:
   * @param f    frequency of the sine wave (hz)
   * @param fs    Sampling rate (hz)
   * @param a       Amplitude
   */

  public DoubleSineGen(double f, double fs, double a) {
    this.fs = fs;
    double w = 2.0 * Math.PI * f / fs;
    this.n0 = 0d;
    this.n1 = a * Math.cos(w + Math.PI/2.0);
    this.k  = 2.0 * Math.cos(w);
  }

  /**
   * Set the new frequency, maintaining the phase
   * @param f       New frequency, hz
   */

  public void setF(double f) {
    double w = 2.0 * Math.PI * f / fs;
    k =  getK(f);

    double theta = Math.acos(n0);
    if (n1 > n0) theta = 2 * Math.PI - theta;
    n0 = Math.cos(theta);
    n1 = Math.cos(w + theta);
  }

  /**
   * Compute the recursion coefficient "k"
   */
  private double getK(double f) {
    double w = 2.0 * Math.PI * f / fs;
    return  2.0 * Math.cos(w);
  }

  /**
   * Generate the next batch of samples.
   * @param samples    Where to put the samples
   * @param start    Start sample
   * @param count    # of samples (must be even)
   */

  public void getSamples(double[] samples, int start, int count) {
    for(int cnt = start; cnt < count; cnt += 2) {
      samples[cnt] = n0 = (k * n1) - n0;
      samples[cnt + 1] = n1 = (k * n0) - n1;
    }
  }
  
  /**
   * Fill the supplied (even length) array with samples.
   */

  public void getSamples(double[] samples) {
    getSamples(samples, 0, samples.length);
  }

  /**
   * Add samples to an existing buffer
   * @param samples    Where to put the samples
   * @param start    Start sample
   * @param count    # of samples (must be even)
   */
  public void addSamples(double[] samples, int start, int count) {
    for(int cnt=start; cnt<count; cnt+=2) {
      samples[cnt] += n0 = (k * n1) - n0;
      samples[cnt + 1] += n1 = (k * n0) - n1;
    }
  }
  
  /**
   * Add samples to the supplied (even length) array.
   */
  public void addSamples(double[] samples) {
    addSamples(samples, 0, samples.length);
  }
  
  /**
   * Get the current sampling frequency.
   */

  public double getFs() {
    return fs;
  }
}




Java Source Code List

com.google.corp.productivity.specialprojects.android.fft.RealDoubleFFT_Mixed.java
com.google.corp.productivity.specialprojects.android.fft.RealDoubleFFT.java
com.google.corp.productivity.specialprojects.android.samples.fft.AnalyzeActivity.java
com.google.corp.productivity.specialprojects.android.samples.fft.AnalyzeView.java
com.google.corp.productivity.specialprojects.android.samples.fft.ColorMapArray.java
com.google.corp.productivity.specialprojects.android.samples.fft.DoubleSineGen.java
com.google.corp.productivity.specialprojects.android.samples.fft.FramesPerSecondCounter.java
com.google.corp.productivity.specialprojects.android.samples.fft.InfoRecActivity.java
com.google.corp.productivity.specialprojects.android.samples.fft.MyPreferences.java
com.google.corp.productivity.specialprojects.android.samples.fft.RecorderMonitor.java
com.google.corp.productivity.specialprojects.android.samples.fft.SBNumFormat.java
com.google.corp.productivity.specialprojects.android.samples.fft.STFT.java
com.google.corp.productivity.specialprojects.android.samples.fft.SelectorText.java
com.google.corp.productivity.specialprojects.android.samples.fft.WavWriter.java