sp_coef_cal.cpp
上传用户:gelin96
上传日期:2017-01-08
资源大小:20993k
文件大小:51k
- /*****************************************************************************
- * Copyright Statement:
- * --------------------
- * This software is protected by Copyright and the information contained
- * herein is confidential. The software may not be copied and the information
- * contained herein may not be used or disclosed except with the written
- * permission of MediaTek Inc. (C) 2005
- *
- * BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
- * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
- * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
- * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
- * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
- * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
- * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
- * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
- * NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
- * SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
- *
- * BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
- * LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
- * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
- * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
- * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
- *
- * THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
- * WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
- * LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
- * RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
- * THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
- *
- *****************************************************************************/
- /*****************************************************************************
- *
- * Filename:
- * ---------
- * sp_coef_cal.cpp
- *
- * Project:
- * --------
- * Maui META APP
- *
- * Description:
- * ------------
- * Acoustic FIR coefficient calibration source
- *
- * Author:
- * -------
- * Andy Ueng (mtk00490)
- *
- *============================================================================
- * HISTORY
- * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
- *------------------------------------------------------------------------------
- * $Revision$
- * $Modtime$
- * $Log$
- *
- *------------------------------------------------------------------------------
- * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
- *============================================================================
- ****************************************************************************/
- #include <IniFiles.hpp>
- #include <stdio.h>
- #include <math.h>
- #pragma hdrstop
- #include "misc.h"
- #include "meta_utils.h"
- #include "man_active.h"
- #include "sp_coef.h"
- #include "sp_coef_cal.h"
- #include "traceconversion.h"
- #define LOG_FLOOR 0.000001
- //===========================================================================
- static CSPCOEFCAL* sp_coef_cal_ptr;
- static bool g_bIsRunning;
- //===========================================================================
- //===========================================================================
- CSPCOEFCAL::CSPCOEFCAL( void )
- {
- g_bIsRunning = false;
- ConfirmCallback = 0;
- CoefCalBufSize = 0;
- CoefCalBuf = 0;
- }
- //---------------------------------------------------------------------------
- CSPCOEFCAL::~CSPCOEFCAL( )
- {
- g_bIsRunning = false;
- ConfirmCallback = 0;
- if(CoefCalBuf)
- {
- delete [] CoefCalBuf;
- CoefCalBuf = NULL;
- }
- AFT_close();
-
- }
- //---------------------------------------------------------------------------
- void CSPCOEFCAL::REQ_Finish( void )
- {
- if(!g_bIsRunning) return;
- Confirm( STATE_COEF_CAL_OK );
- g_bIsRunning = false;
- }
- //---------------------------------------------------------------------------
- void CSPCOEFCAL::REQ_Stop( void )
- {
- if(!g_bIsRunning) return;
- META_Cancel_r( m_META_HANDLE_Obj.Get_MainHandle(), SPID_COEF_CAL );
- Confirm( STATE_COEF_CAL_STOP );
- g_bIsRunning = false;
- }
- //---------------------------------------------------------------------------
- void CSPCOEFCAL::REQ_TimeOut( void )
- {
- if(!g_bIsRunning) return;
- META_Cancel_r( m_META_HANDLE_Obj.Get_MainHandle(), SPID_COEF_CAL );
- Confirm( STATE_COEF_CAL_TIMEOUT );
- g_bIsRunning = false;
- }
- //---------------------------------------------------------------------------
- void CSPCOEFCAL::Confirm( int confirm_state )
- {
- if(!g_bIsRunning) return;
- if(ConfirmCallback==0) return;
- ConfirmState = confirm_state;
- ActiveMan->SetActiveFunction( ConfirmCallback );
- }
- //---------------------------------------------------------------------------
- bool CSPCOEFCAL::REQ_Read_TRC_File( char *filename, double* p_TrcScale, sSpCoefCalParameters *p_SpCoefCal )
- {
- FILE *fp;
- unsigned int i;
- char str[512];
- AnsiString as_Str;
- bool isMatch = false;
- for( i=0; i<TOTAL_TRC_FREQ_NUM; i++ )
- {
- p_SpCoefCal->Hz[i] = 0;
- p_SpCoefCal->Mag[i] = 0;
- p_SpCoefCal->MagdB[i] = 0;
- }
- fp = fopen(filename, "r");
- if(fp == NULL)
- return false;
- for( i=0; i<TRC_FREQ_POSITION; i++ )
- {
- if( feof(fp) )
- return false;
- fscanf( fp, "%s", &str);
- }
- as_Str = str;
- p_SpCoefCal->num = as_Str.ToInt();
- while(!feof(fp))
- {
- fscanf( fp, "%s", &str);
- if( strcmpi(str, "#----X-------------Y---------") == 0)
- {
- isMatch = true;
- break;
- }
- }
- if( !isMatch )
- {
- fclose(fp);
- return false;
- }
- for( i=0; i<2*p_SpCoefCal->num; i++ )
- {
- fscanf( fp, "%s", &str);
- if( i % 2 == 0 )
- {
- as_Str = str;
- p_SpCoefCal->Hz[i/2] = as_Str.ToInt();
- }
- else
- {
- as_Str = str;
- p_SpCoefCal->Mag[i/2] = atof(as_Str.c_str());
- // getYfromStr( as_Str, &TrcFreq[i] );
- }
- }
- for( i=0; i<SCALING_FACTOR_POSITION; i++)
- {
- fscanf( fp, "%s", &str);
- }
- as_Str = str;
- *p_TrcScale = atof(as_Str.c_str());
- if( *p_TrcScale == 0.0 ) *p_TrcScale = 1.0;
- if( p_SpCoefCal->Hz[p_SpCoefCal->num-1] > 3960.0 )
- {
- p_SpCoefCal->num--;
- }
- for( i=0; i<p_SpCoefCal->num; i++ )
- {
- p_SpCoefCal->Mag[i] = p_SpCoefCal->Mag[i] / (*p_TrcScale);
- p_SpCoefCal->MagdB[i] = 20.0 * log10(p_SpCoefCal->Mag[i]);
- sprintf(str, "%.3f", p_SpCoefCal->MagdB[i]);
- as_Str = (AnsiString) str;
- p_SpCoefCal->MagdB[i] = atof(as_Str.c_str());
- }
- fclose(fp);
- return true;
- }
- //==============================================================================
- bool CSPCOEFCAL::REQ_Read_FIR_File( char *filename, bool b_45TapSupport, sFirCoefParameters *p_FirCoef )
- {
- FILE *fp;
- char str[512];
- AnsiString as_Str;
- fp = fopen(filename, "r");
- if(fp == NULL)
- return false;
- // get taps
- fscanf( fp, "%s", &str);
- as_Str = (AnsiString) str;
- p_FirCoef->taps = as_Str.ToInt();
- if( b_45TapSupport )
- {
- if( p_FirCoef->taps > SPEECH_FIR_45_TAPS_NUM )
- {
- p_FirCoef->taps = SPEECH_FIR_45_TAPS_NUM;
- }
- }
- else
- {
- if( p_FirCoef->taps > SPEECH_FIR_30_TAPS_NUM )
- {
- p_FirCoef->taps = SPEECH_FIR_30_TAPS_NUM;
- }
- }
- for (unsigned int i=0; i<p_FirCoef->taps; i++)
- {
- fscanf( fp, "%s", &str);
- as_Str = (AnsiString) str;
- p_FirCoef->FirCoef[i] = atof(as_Str.c_str());
- }
- if( p_FirCoef->taps <= 0 )
- {
- p_FirCoef->taps = 4;
- p_FirCoef->FirCoef[0] = FIR_COEFF_SCALE;
- p_FirCoef->FirCoef[1] = 0;
- p_FirCoef->FirCoef[2] = 0;
- p_FirCoef->FirCoef[3] = 0;
- }
- fclose(fp);
- return true;
- }
- //------------------------------------------------------------------------------
- bool CSPCOEFCAL::REQ_Write_FIR_File( char *filename, sFirCoefParameters *p_FirCoef )
- {
- FILE *fp;
- char str[512];
- AnsiString as_Str;
- fp = fopen(filename, "w");
- if(fp == NULL)
- return false;
- fprintf( fp, "%dn", (int) p_FirCoef->taps);
- for (unsigned int i=0; i<p_FirCoef->taps; i++)
- {
- fprintf( fp, "%dn", (int) p_FirCoef->FirCoef[i]);
- }
- fclose(fp);
- return true;
- }
- #if 0
- //------------------------------------------------------------------------------
- bool CSPCOEFCAL::CheckLicenseDat( void )
- {
- FILE *fp;
- AnsiString asPath, asFileName;
- if ( getPathFromStr(Application->ExeName, asPath) &&
- withPath( asPath) &&
- !withPath( asFileName)
- )
- {
- asFileName = asPath + "license.dat";
- }
- fp = fopen(asFileName.c_str(), "r");
- if( fp == NULL )
- {
- fclose(fp);
- return false;
- }
- fclose(fp);
- return true;
- }
- #endif
- //==============================================================================
- void CSPCOEFCAL::ReorderFirCoef(sFirCoefParameters *p_FirCoef)
- {
- double temp_old_fir_coeffs[SPEECH_FIR_45_TAPS_NUM];
- for (unsigned int i=0; i<p_FirCoef->taps; i++ )
- {
- temp_old_fir_coeffs[i] = p_FirCoef->FirCoef[i];
- }
- for (unsigned int i=0; i<p_FirCoef->taps; i++)
- {
- p_FirCoef->FirCoef[i] = temp_old_fir_coeffs[p_FirCoef->taps-1-i];
- }
- }
- //==============================================================================
- void CSPCOEFCAL::getSelHzIndex(int *p_TplHz, int *p_SelHz, int* p_SelHzIndex)
- {
- int k=0;
- for( int i=0; i<TOTAL_TRC_FREQ_NUM; i++ )
- {
- for( int j=0; j<TOTAL_SEL_FREQ_NUM; j++)
- {
- if( *(p_TplHz+i) == *(p_SelHz+j) )
- {
- p_SelHzIndex[k]=i;
- k++;
- break;
- }
- }
- }
- }
- //-----------------------------------------------------------------------------
- void CSPCOEFCAL::getSeldB( double *p_TpldB, double *p_SeldB, int* p_SelHzIndex)
- {
- for(int i=0; i<TOTAL_SEL_FREQ_NUM; i++)
- {
- *(p_SeldB+i) = *(p_TpldB+*(p_SelHzIndex+i));
- }
- }
- //==============================================================================
- #if 0
- bool CSPCOEFCAL::CheckEngineExist( void )
- {
- // engine test code
- Engine *mat;
- mat = engOpen( "