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 from calc_thd import * 21 import calc_delay 22 23 # calculate THD for dut_recording_thd case 24 # Input: host recording (mono), device recording (mono), 25 # frequency of sine in Hz (i64) 26 # THD pass level in percentile (double) 27 # Output:THD host (double), THD device (double) in percentile 28 # host recording will be longer than device recording 29 # the function works in following steps: 30 # 1. match the start of device recording with host recording 31 # As the host recording starts eariler and longer than device recording, 32 # matching process is required. 33 # 2. calculate THD of host recording and client recording 34 # 3. check pass/fail 35 36 def recording_thd(inputData, inputTypes): 37 output = [] 38 outputData = [] 39 outputTypes = [] 40 # basic sanity check 41 inputError = False 42 if (inputTypes[0] != TYPE_MONO): 43 inputError = True 44 if (inputTypes[1] != TYPE_MONO): 45 inputError = True 46 if (inputTypes[2] != TYPE_I64): 47 inputError = True 48 if (inputTypes[3] != TYPE_DOUBLE): 49 inputError = True 50 if inputError: 51 output.append(RESULT_ERROR) 52 output.append(outputData) 53 output.append(outputTypes) 54 return output 55 56 hostRecording = inputData[0] 57 deviceRecording = inputData[1] 58 signalFrequency = inputData[2] 59 thdPassPercentile = inputData[3] 60 samplingRate = 44100 61 62 delay = calc_delay.calc_delay(hostRecording, deviceRecording) 63 N = len(deviceRecording) 64 print "delay ", delay, "deviceRecording samples ", N 65 thdHost = calc_thd(hostRecording[delay:delay+N], signalFrequency, samplingRate, 0.02) * 100 66 thdDevice = calc_thd(deviceRecording, signalFrequency, samplingRate, 0.02) * 100 67 print "THD Host %", thdHost, "THD device %", thdDevice, "Margain % ", thdPassPercentile 68 if (thdDevice < (thdHost + thdPassPercentile)) and (thdHost < thdPassPercentile): 69 output.append(RESULT_PASS) 70 else: 71 output.append(RESULT_OK) 72 outputData.append(thdHost) 73 outputTypes.append(TYPE_DOUBLE) 74 outputData.append(thdDevice) 75 outputTypes.append(TYPE_DOUBLE) 76 output.append(outputData) 77 output.append(outputTypes) 78 return output 79