Keun young Park | 4bbb5d7 | 2012-03-26 18:31:29 -0700 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | # Copyright (C) 2012 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | from consts import * |
| 18 | import numpy as np |
| 19 | import scipy as sp |
| 20 | import scipy.fftpack as fft |
| 21 | import matplotlib.pyplot as plt |
| 22 | |
| 23 | # generate random signal |
| 24 | # Input: peak amplitude, |
| 25 | # duration in msec, |
| 26 | # sampling rate HZ |
| 27 | # low frequency, |
| 28 | # high frequency, |
| 29 | # Output: generated sound (stereo) |
| 30 | |
| 31 | def do_gen_random(peakAmpl, durationInMSec, samplingRate, fLow, fHigh, stereo=True): |
| 32 | samples = durationInMSec * samplingRate / 1000 |
| 33 | result = np.zeros(samples * 2 if stereo else samples, dtype=np.int16) |
| 34 | #randomSignal = np.random.random_integers(-peakAmpl, peakAmpl, samples) |
| 35 | randomSignal = np.random.normal(scale=peakAmpl *2 / 3, size=samples) |
| 36 | fftData = fft.rfft(randomSignal) |
| 37 | freqSamples = samples/2 |
| 38 | iLow = 0 #freqSamples * fLow * 2/ samplingRate + 1 |
| 39 | #if iLow > freqSamples - 1: |
| 40 | # iLow = freqSamples - 1 |
| 41 | iHigh = freqSamples * fHigh * 2 / samplingRate + 1 |
| 42 | #print len(randomSignal), len(fftData), fLow, iLow, fHigh, iHigh |
| 43 | if iHigh > freqSamples - 1: |
| 44 | iHigh = freqSamples - 1 |
| 45 | fftData[0] = 0 # DC |
| 46 | #for i in range(iLow - 1): |
| 47 | # fftData[2 * i + 1 ] = 0 # Re |
| 48 | # fftData[2 * i + 2 ] = 0 # Imag |
| 49 | for i in range(iHigh, freqSamples - 1): |
| 50 | fftData[ 2 * i + 1 ] = 0 |
| 51 | fftData[ 2 * i + 2 ] = 0 |
| 52 | if (samples - 2 *freqSamples) != 0: |
| 53 | fftData[samples - 1] = 0 |
| 54 | |
| 55 | filteredData = fft.irfft(fftData) |
| 56 | #freq = np.linspace(0.0, samplingRate, num=len(fftData), endpoint=False) |
| 57 | #plt.plot(freq, abs(fft.fft(filteredData))) |
| 58 | #plt.plot(filteredData) |
| 59 | #plt.show() |
| 60 | if stereo: |
| 61 | for i in range(len(filteredData)): |
| 62 | result[2 * i] = filteredData[i] |
| 63 | result[2 * i + 1] = filteredData[i] |
| 64 | else: |
| 65 | for i in range(len(filteredData)): |
| 66 | result[i] = filteredData[i] |
| 67 | return result |
| 68 | |
| 69 | |
| 70 | def gen_random(inputData, inputTypes): |
| 71 | output = [] |
| 72 | outputData = [] |
| 73 | outputTypes = [] |
| 74 | # basic sanity check |
| 75 | inputError = False |
| 76 | if (inputTypes[0] != TYPE_I64): |
| 77 | inputError = True |
| 78 | if (inputTypes[1] != TYPE_I64): |
| 79 | inputError = True |
| 80 | if (inputTypes[2] != TYPE_I64): |
| 81 | inputError = True |
| 82 | if (inputTypes[3] != TYPE_I64): |
| 83 | inputError = True |
| 84 | if (inputTypes[4] != TYPE_I64): |
| 85 | inputError = True |
| 86 | if inputError: |
| 87 | output.append(RESULT_ERROR) |
| 88 | output.append(outputData) |
| 89 | output.append(outputTypes) |
| 90 | return output |
| 91 | |
| 92 | result = do_gen_random(inputData[0], inputData[1], inputData[2], inputData[3], inputData[4]) |
| 93 | |
| 94 | output.append(RESULT_OK) |
| 95 | outputData.append(result) |
| 96 | outputTypes.append(TYPE_STEREO) |
| 97 | output.append(outputData) |
| 98 | output.append(outputTypes) |
| 99 | return output |
| 100 | |
| 101 | # test code |
| 102 | if __name__=="__main__": |
| 103 | peakAmplitude = 10000 |
| 104 | samplingRate = 44100 |
| 105 | durationInMSec = 10000 |
| 106 | fLow = 500 |
| 107 | fHigh = 15000 |
| 108 | result = do_gen_random(peakAmplitude, durationInMSec, samplingRate, fLow, fHigh) |
| 109 | plt.plot(result) |
| 110 | plt.show() |