Home | History | Annotate | Download | only in loopback
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package org.drrickorang.loopback;
     18 
     19 
     20 /**
     21  * This class contains functions that can be reused in different classes.
     22  */
     23 
     24 public class Utilities {
     25 
     26 
     27     /** Multiply the input array with a hanning window. */
     28     public static void hanningWindow(double[] samples) {
     29         int length = samples.length;
     30         final double alpha = 0.5;
     31         final double beta = 0.5;
     32         double coefficient;
     33         for (int i = 0; i < length; i++) {
     34             coefficient = (Constant.TWO_PI * i) / (length - 1);
     35             samples[i] *= alpha - beta * Math.cos(coefficient);
     36         }
     37 
     38     }
     39 
     40 
     41     /** Round up to the nearest power of 2. */
     42     public static int roundup(int size)
     43     {
     44         // Integer.numberOfLeadingZeros() returns 32 for zero input
     45         if (size == 0) {
     46             size = 1;
     47         }
     48 
     49         int lz = Integer.numberOfLeadingZeros(size);
     50         int rounded = 0x80000000 >>> lz;
     51         // 0x800000001 and higher are actually rounded _down_ to prevent overflow
     52         if (size > rounded && lz > 0) {
     53             rounded <<= 1;
     54         }
     55         return rounded;
     56     }
     57 
     58 }
     59