Android Open Source - rfcx-guardian-android L P C






From Project

Back to project page rfcx-guardian-android.

License

The source code is released under:

Apache License

If you think the Android project rfcx-guardian-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 (C) 2010  Preston Lacey http://javaflacencoder.sourceforge.net/
 * All Rights Reserved./* ww w . j  a va2s. c  om*/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

package net.sourceforge.javaFlacEncoder;

/**
 * This class is used to calculate LPC Coefficients for a FLAC stream.
 *
 * @author Preston Lacey
 */
public class LPC {
  /** The error calculated by the LPC algorithm */
  protected double rawError;
  /** The coefficients as calculated by the LPC algorithm */
  protected double[] rawCoefficients;
  private double[] tempCoefficients;
  /** The order of this LPC calculation */
  protected int order;
  static int[] tempSum = null;
  /**
   * Constructor creates an LPC object of the given order.
   * @param order Order for this LPC calculation.
   */
  public LPC(int order) {
    this.order = order;
    rawError = 0;
    rawCoefficients = new double[order+1];
    tempCoefficients = new double[order+1];
  }
  /**
   * Get this LPC object's order
   * @return order used for this LPC calculation.
   */
  public int getOrder () { return order; }
  /**
   * Get the error for this LPC calculation
   * @return lpc error
   */
  public double getError() { return rawError; }

  /**
   * Get the calculated LPC Coefficients as an array.
   * @return lpc coefficients in an array.
   */
  public double[] getCoefficients() { return rawCoefficients; }

  /**
   * Calculate an LPC using the given Auto-correlation data. Static method
   * used since this is slightly faster than a more strictly object-oriented
   * approach.
   *
   * @param lpc LPC to calculate
   * @param R Autocorrelation data to use
   */
  public static void calculate(LPC lpc, long[] R) {
    int coeffCount = lpc.order;

    //calculate first iteration directly
    double[] A = lpc.rawCoefficients;
    for(int i = 0; i < coeffCount+1; i++) A[i] = 0.0;
    A[0] = 1;
    double E = R[0];

    //calculate remaining iterations

    if(R[0] == 0) {
      for(int i = 0; i < coeffCount+1; i++)
        A[i] = 0.0;
    }
    else {
      double[] ATemp = lpc.tempCoefficients;
      for(int i = 0; i < coeffCount+1; i++) ATemp[i] = 0.0;

      for(int k = 0; k < coeffCount; k++) {
        double lambda = 0.0;
        double temp = 0;
        for(int j = 0; j <= k; j++) {
          temp += A[j]*R[k+1-j];
        }
        lambda = -temp/E;

        for(int i = 0; i <= k+1; i++) {
          ATemp[i] = A[i]+lambda*A[k+1-i];
        }
        System.arraycopy(ATemp, 0, A, 0, coeffCount+1);
        E = (1-lambda*lambda)*E;
      }
    }
    lpc.rawError = E;
  }

  /**
   * Calculate an LPC using a prior order LPC's values to save calculations.
   *
   * @param lpc LPC to calculate
   * @param R Auto-correlation data to use.
   * @param priorLPC Prior order LPC to use(may be any order lower than our
   * target LPC)
   *
   */
  public static void calculateFromPrior(LPC lpc, long[] R, LPC priorLPC) {
    int coeffCount = lpc.order;

    //calculate first iteration directly
    double[] A = lpc.rawCoefficients;
    for(int i = 0; i < coeffCount+1; i++) A[i] = 0.0;
    A[0] = 1;
    double E = R[0];
    int startIter = 0;
    if(priorLPC != null && priorLPC.order < lpc.order) {
      startIter = priorLPC.order;
      E = priorLPC.rawError;
      System.arraycopy(priorLPC.rawCoefficients, 0, A, 0, startIter+1);
    }
    //calculate remaining iterations
    if(R[0] == 0) {
      for(int i = 0; i < coeffCount+1; i++)
        A[i] = 0.0;
    }
    else {
      double[] ATemp = lpc.tempCoefficients;
      for(int i = 0; i < coeffCount+1; i++) ATemp[i] = 0.0;

      for(int k = startIter; k < coeffCount; k++) {
        double lambda = 0.0;
        double temp = 0.0;
        for(int j = 0; j <= k; j++) {
          temp -= A[j]*R[k-j+1];
        }
        lambda = temp/E;

        for(int i = 0; i <= k+1; i++) {
          ATemp[i] = A[i]+lambda*A[k+1-i];
        }
        System.arraycopy(ATemp, 0, A, 0, coeffCount+1);
        E = (1-lambda*lambda)*E;
      }
    }
    lpc.rawError = E;
  }

  /**
   * Create auto-correlation coefficients(up to a maxOrder of 32).
   * @param R Array to put results in.
   * @param samples Samples to calculate the auto-correlation for.
   * @param count number of samples to use
   * @param start index of samples array to start at
   * @param increment number of indices to increment between valid samples(for
   * interleaved arrays)
   * @param maxOrder maximum order to calculate.
   */
  public static void createAutoCorrelation(long[] R, int []samples, int count,
      int start, int increment, int maxOrder) {
    if(increment == 1 && start == 0) {
      for(int i = 0; i <= maxOrder; i++) {
        R[i] = 0;
        long temp = 0;
        for(int j = 0; j < count-i; j++) {
          temp += (long)samples[j]*(long)samples[j+i];
        }
        R[i] += temp;
      }
    }
    else {
      for(int i = 0; i <= maxOrder; i++) {
        R[i] = 0;
        int baseIndex = increment*i;
        long temp = 0;
        int innerLimit = (count-i)*increment;
        for(int j = start; j < innerLimit; j+=increment) {
          temp += (long)samples[j]*(long)samples[j+baseIndex];
        }
        R[i] += temp;
      }
    }
  }
 
  /**
   * Apply a window function to sample data
   * @param samples Samples to apply window to. Values in this array are left
   * unaltered.
   * @param count number of samples to use
   * @param start index of samples array to start at
   * @param increment number of indices to increment between valid samples(for
   * interleaved arrays)
   * @param windowedSamples array containing windowed values. Return values
   * are packed(increment of one).
   *
   */
  public static void window(int[] samples, int count, int start, int increment,
      int[] windowedSamples) {
    int[] values = windowedSamples;
    int loopCount = 0;
    float halfway = count/2.0f;
    float hth = halfway*halfway;
    float windowCount = -halfway;
    int limit = count*increment+start;
    for(int i = start; i < limit; i+=increment) {
      //float innerCount = (windowCount < 0) ? -windowCount:windowCount;
      float innerCountSquared = windowCount*windowCount;
      windowCount++;
      //double val = 1.0-(double)(innerCount/halfway);
      float val = 1.0f-( innerCountSquared/hth );
      double temp = ((double)samples[i])*val;
      temp = (temp >0) ? temp+0.5:temp-0.5;
      values[loopCount++] = (int)temp;
    }
  }

}




Java Source Code List

net.sourceforge.javaFlacEncoder.ArrayRecycler.java
net.sourceforge.javaFlacEncoder.BlockEncodeRequest.java
net.sourceforge.javaFlacEncoder.BlockThreadManager.java
net.sourceforge.javaFlacEncoder.CRC16.java
net.sourceforge.javaFlacEncoder.CRC8.java
net.sourceforge.javaFlacEncoder.ChannelData.java
net.sourceforge.javaFlacEncoder.EncodedElement_32.java
net.sourceforge.javaFlacEncoder.EncodedElement.java
net.sourceforge.javaFlacEncoder.EncodingConfiguration.java
net.sourceforge.javaFlacEncoder.FLACEncoder.java
net.sourceforge.javaFlacEncoder.FLACFileOutputStream.java
net.sourceforge.javaFlacEncoder.FLACOutputStream.java
net.sourceforge.javaFlacEncoder.FLACStreamController.java
net.sourceforge.javaFlacEncoder.FLACStreamIdentifier.java
net.sourceforge.javaFlacEncoder.FLACStreamOutputStream.java
net.sourceforge.javaFlacEncoder.FLAC_ConsoleFileEncoder.java
net.sourceforge.javaFlacEncoder.FLAC_FileEncoder.java
net.sourceforge.javaFlacEncoder.FLAC_MD5.java
net.sourceforge.javaFlacEncoder.FrameHeader.java
net.sourceforge.javaFlacEncoder.FrameThread.java
net.sourceforge.javaFlacEncoder.Frame.java
net.sourceforge.javaFlacEncoder.LPC.java
net.sourceforge.javaFlacEncoder.MetadataBlockHeader.java
net.sourceforge.javaFlacEncoder.MetadataBlockStreamInfo.java
net.sourceforge.javaFlacEncoder.RiceEncoder.java
net.sourceforge.javaFlacEncoder.StreamConfiguration.java
net.sourceforge.javaFlacEncoder.Subframe_Constant.java
net.sourceforge.javaFlacEncoder.Subframe_Fixed.java
net.sourceforge.javaFlacEncoder.Subframe_LPC.java
net.sourceforge.javaFlacEncoder.Subframe_Verbatim.java
net.sourceforge.javaFlacEncoder.Subframe.java
net.sourceforge.javaFlacEncoder.UTF8Modified.java
org.rfcx.guardian.RfcxGuardianPrefs.java
org.rfcx.guardian.RfcxGuardian.java
org.rfcx.guardian.activity.MainActivity.java
org.rfcx.guardian.activity.PrefsActivity.java
org.rfcx.guardian.api.ApiCore.java
org.rfcx.guardian.audio.AudioCore.java
org.rfcx.guardian.database.AlertDb.java
org.rfcx.guardian.database.AudioDb.java
org.rfcx.guardian.database.DeviceStateDb.java
org.rfcx.guardian.database.SmsDb.java
org.rfcx.guardian.device.AirplaneMode.java
org.rfcx.guardian.device.CpuUsage.java
org.rfcx.guardian.device.DeviceState.java
org.rfcx.guardian.intentservice.ApiCheckInTriggerIntentService.java
org.rfcx.guardian.intentservice.AudioEncodeIntentService.java
org.rfcx.guardian.intentservice.ServiceMonitorIntentService.java
org.rfcx.guardian.receiver.AirplaneModeReceiver.java
org.rfcx.guardian.receiver.BootReceiver.java
org.rfcx.guardian.receiver.ConnectivityReceiver.java
org.rfcx.guardian.receiver.SmsReceiver.java
org.rfcx.guardian.service.ApiCheckInService.java
org.rfcx.guardian.service.AudioCaptureService.java
org.rfcx.guardian.service.CarrierCodeService.java
org.rfcx.guardian.service.DeviceStateService.java
org.rfcx.guardian.telecom.CarrierInteraction.java
org.rfcx.guardian.utility.DateTimeUtils.java
org.rfcx.guardian.utility.DeviceGuid.java
org.rfcx.guardian.utility.ExtAudioRecorderModified.java
org.rfcx.guardian.utility.FileUtils.java
org.rfcx.guardian.utility.HttpGet.java
org.rfcx.guardian.utility.HttpPostMultipart.java
org.rfcx.guardian.utility.TimeOfDay.java