form_FirCoeffCal.cpp
上传用户:gelin96
上传日期:2017-01-08
资源大小:20993k
文件大小:151k
- /*****************************************************************************
- * 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:
- * ---------
- * form_FirCoeffCal.cpp
- *
- * Project:
- * --------
- * Maui META APP
- *
- * Description:
- * ------------
- * FIR coefficient calibration form 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 <vcl.h>
- #include <math.h>
- #include <IniFiles.hpp>
- #include <SyncObjs.hpp>
- //#include "freqz.hpp"
- //#include "libmatlb.h"
- //#include "freqz.h"
- //#include "psdoptions.h"
- //#include "engine.h"
- //firls
- //#include "firls.h"
- #pragma hdrstop
- #include "form_FirResponse.h"
- #ifndef _FORM_MAIN_H_
- #include "form_main.h"
- #endif
- #include "sp_coef_cal.h"
- #include "form_FirCoeffCal.h"
- #include "man_dll.h"
- #include "man_active.h"
- #include "meta_msg.h"
- #include "meta_utils.h"
- // misc
- #ifndef _MISC_AUDIO_H_
- #include "misc_audio.h"
- #endif
- #ifndef _FT_UTILS_H_
- #include "ft_utils.h"
- #endif
- #ifndef _MATH_UTILS_H_
- #include "math_utils.h"
- #endif
- // file
- #ifndef _MFSETUPUTIL_H_
- #include "mfsetuputil.h"
- #endif
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- typedef enum
- {
- ITEMIDX_BOUNDARY_SPEC = 0,
- ITEMIDX_BOUNDARY_GOOD_SOUND,
- ITEMIDX_BOUNDARY_3G_HANDSET_AND_HEADSET,
- ITEMIDX_BOUNDARY_3G_HANDSFREE_DESKTOP_AND_VEHICLE,
- ITEMIDX_BOUNDARY_3G_HANDSFREE_HANDHELD,
- ITEMIDX_BOUNDARY_VOIP_HANDSET,
- ITEMIDX_BOUNDARY_VOIP_HEADSET,
- ITEMIDX_BOUNDARY_VOIP_HANDSFREE,
- ITEMIDX_BOUNDARY_TYPE_3_3
- }E_ITEMIDX_BOUNDARY_T;
- typedef enum
- {
- ACOUSTIC_EQUIPMENT_UPL = 0,
- ACOUSTIC_EQUIPMENT_ACQUA,
- ACOUSTIC_EQUIPMENT_MICROTRONIX,
- ACOUSTIC_EQUIPMENT_VITA
- }E_ACOUSTIC_EQUIPMENT_T;
- //---------------------------------------------------------------------------
- TfrmFIRCoeffCal *frmFIRCoeffCal;
- //---------------------------------------------------------------------------
- void Req_freqz(void)
- {
- frmFIRCoeffCal->Req_freqz();
- }
- //==============================================================================
- void _fastcall TfrmFIRCoeffCal::SubClassWndProc(Messages::TMessage &Message)
- {
- switch (Message.Msg)
- {
- default:
- {
- this->WndProc(Message);
- }
- break;
- }
- }
- //---------------------------------------------------------------------------
- __fastcall TfrmFIRCoeffCal::TfrmFIRCoeffCal(TComponent* Owner)
- : TForm(Owner)
- {
- int i;
- // initialization
- FirScale = 0.5;
- isTRCLoadOk = false;
- isOrgCurvePlot = false;
- isWantedCurvePlot = false;
- isCompensateCurvePlot = false;
- LogFloor = pow(10, -10);
- pi = 3.1416;
- lblVertical[0] = lblVertical0;
- lblVertical[1] = lblVertical1;
- lblVertical[2] = lblVertical2;
- lblVertical[3] = lblVertical3;
- lblVertical[4] = lblVertical4;
- lblVertical[5] = lblVertical5;
- lblVertical[6] = lblVertical6;
- lblVertical[7] = lblVertical7;
- lblVertical[8] = lblVertical8;
- lblHz[0] = lbl1xxHz;
- lblHz[1] = lbl2xxHz;
- lblHz[2] = lbl3xxHz;
- lblHz[3] = lbl5xxHz;
- lblHz[4] = lbl6xxHz;
- lblHz[5] = lbl7xxHz;
- lblHz[6] = lbl8xxHz;
- lblHz[7] = lbl1xxxHz;
- lblHz[8] = lbl13xxHz;
- lblHz[9] = lbl18xxHz;
- lblHz[10] = lbl2xxxHz;
- lblHz[11] = lbl25xxHz;
- lblHz[12] = lbl3xxxHz;
- lblHz[13] = lbl34xxHz;
- lblHz[14] = lbl39xxHz;
- edtHzdB[0] = edt1xxHzdB;
- edtHzdB[1] = edt2xxHzdB;
- edtHzdB[2] = edt3xxHzdB;
- edtHzdB[3] = edt5xxHzdB;
- edtHzdB[4] = edt6xxHzdB;
- edtHzdB[5] = edt7xxHzdB;
- edtHzdB[6] = edt8xxHzdB;
- edtHzdB[7] = edt1xxxHzdB;
- edtHzdB[8] = edt13xxHzdB;
- edtHzdB[9] = edt18xxHzdB;
- edtHzdB[10] = edt2xxxHzdB;
- edtHzdB[11] = edt25xxHzdB;
- edtHzdB[12] = edt3xxxHzdB;
- edtHzdB[13] = edt34xxHzdB;
- edtHzdB[14] = edt39xxHzdB;
- m_dTxUpLimit[0] = -12; // 100 Hz
- m_dTxUpLimit[1] = 0; // 200 Hz
- m_dTxUpLimit[2] = 0; // 300 Hz
- m_dTxUpLimit[3] = 0; // 1000 Hz
- m_dTxUpLimit[4] = 4; // 2000 Hz
- m_dTxUpLimit[5] = 4; // 3000 Hz
- m_dTxUpLimit[6] = 4; // 3400 Hz
- m_dTxUpLimit[7] = 0; // 4000 Hz
- m_iTxLimitFreq[0] = 100;
- m_iTxLimitFreq[1] = 200;
- m_iTxLimitFreq[2] = 300;
- m_iTxLimitFreq[3] = 1000;
- m_iTxLimitFreq[4] = 2000;
- m_iTxLimitFreq[5] = 3000;
- m_iTxLimitFreq[6] = 3400;
- m_iTxLimitFreq[7] = 4000;
- // UITxLimitFreq[0] = 100;
- // UITxLimitFreq[1] = 200;
- // UITxLimitFreq[2] = 300;
- // UITxLimitFreq[3] = 1000;
- // UITxLimitFreq[4] = 2000;
- // UITxLimitFreq[5] = 3000;
- // UITxLimitFreq[6] = 3400;
- // UITxLimitFreq[7] = 4000;
- m_dTxLowLimit[0] = -100; // 100 Hz
- m_dTxLowLimit[1] = -100; // 200 Hz
- m_dTxLowLimit[2] = -12; // 300 Hz
- m_dTxLowLimit[3] = -6; // 1000 Hz
- m_dTxLowLimit[4] = -6; // 2000 Hz
- m_dTxLowLimit[5] = -6; // 3000 Hz
- m_dTxLowLimit[6] = -9; // 3400 Hz
- m_dTxLowLimit[7] = -100; // 4000 Hz
- m_dRxUpLimit[0] = -12; // 100 Hz
- m_dRxUpLimit[1] = 0; // 200 Hz
- m_dRxUpLimit[2] = 2; // 300 Hz
- m_dRxUpLimit[3] = 2+(20*log10(500)-20*log10(300))*(0-2)/(20*log10(1000)-20*log10(300)); // 500 Hz
- m_dRxUpLimit[4] = 0; // 1000 Hz
- m_dRxUpLimit[5] = 2; // 3000 Hz
- m_dRxUpLimit[6] = 2; // 3400 Hz
- m_dRxUpLimit[7] = 2; // 4000 Hz
- m_iRxLimitFreq[0] = 100;
- m_iRxLimitFreq[1] = 200;
- m_iRxLimitFreq[2] = 300;
- m_iRxLimitFreq[3] = 500;
- m_iRxLimitFreq[4] = 1000;
- m_iRxLimitFreq[5] = 3000;
- m_iRxLimitFreq[6] = 3400;
- m_iRxLimitFreq[7] = 4000;
- // UIRxLimitFreq[0] = 100;
- // UIRxLimitFreq[1] = 200;
- // UIRxLimitFreq[2] = 300;
- // UIRxLimitFreq[3] = 500;
- // UIRxLimitFreq[4] = 1000;
- // UIRxLimitFreq[5] = 3000;
- // UIRxLimitFreq[6] = 3400;
- // UIRxLimitFreq[7] = 4000;
- m_dRxLowLimit[0] = -100; // 100 Hz
- m_dRxLowLimit[1] = -100; // 200 Hz
- m_dRxLowLimit[2] = -7; // 300 Hz
- m_dRxLowLimit[3] = -5; // 500 Hz
- m_dRxLowLimit[4] = -5; // 1000 Hz
- m_dRxLowLimit[5] = -5; // 3000 Hz
- m_dRxLowLimit[6] = -10; // 3400 Hz
- m_dRxLowLimit[7] = -100; // 4000 Hz
- // UITxUpLimit[0] = m_dTxUpLimit[0]; // 100 Hz
- // UITxUpLimit[1] = m_dTxUpLimit[1]; // 200 Hz
- // UITxUpLimit[2] = m_dTxUpLimit[2]; // 300 Hz
- // UITxUpLimit[3] = m_dTxUpLimit[3]; // 1000 Hz
- // UITxUpLimit[4] = m_dTxUpLimit[4]; // 2000 Hz
- // UITxUpLimit[5] = m_dTxUpLimit[5]; // 3000 Hz
- // UITxUpLimit[6] = m_dTxUpLimit[6]; // 3400 Hz
- // UITxUpLimit[7] = m_dTxUpLimit[7]; // 4000 Hz
- // UITxLowLimit[0] = m_dTxLowLimit[0]; // 100 Hz
- // UITxLowLimit[1] = m_dTxLowLimit[1]; // 200 Hz
- // UITxLowLimit[2] = m_dTxLowLimit[2]; // 300 Hz
- // UITxLowLimit[3] = m_dTxLowLimit[3]; // 1000 Hz
- // UITxLowLimit[4] = m_dTxLowLimit[4]; // 2000 Hz
- // UITxLowLimit[5] = m_dTxLowLimit[5]; // 3000 Hz
- // UITxLowLimit[6] = m_dTxLowLimit[6]; // 3400 Hz
- // UITxLowLimit[7] = m_dTxLowLimit[7]; // 4000 Hz
- // UIRxUpLimit[0] = m_dRxUpLimit[0]; // 100 Hz
- // UIRxUpLimit[1] = m_dRxUpLimit[1]; // 200 Hz
- // UIRxUpLimit[2] = m_dRxUpLimit[2]; // 300 Hz
- // UIRxUpLimit[3] = m_dRxUpLimit[3]; // 500 Hz
- // UIRxUpLimit[4] = m_dRxUpLimit[4]; // 1000 Hz
- // UIRxUpLimit[5] = m_dRxUpLimit[5]; // 3000 Hz
- // UIRxUpLimit[6] = m_dRxUpLimit[6]; // 3400 Hz
- // UIRxUpLimit[7] = m_dRxUpLimit[7]; // 4000 Hz
- // UIRxLowLimit[0] = m_dRxLowLimit[0]; // 100 Hz
- // UIRxLowLimit[1] = m_dRxLowLimit[1]; // 200 Hz
- // UIRxLowLimit[2] = m_dRxLowLimit[2]; // 300 Hz
- // UIRxLowLimit[3] = m_dRxLowLimit[3]; // 500 Hz
- // UIRxLowLimit[4] = m_dRxLowLimit[4]; // 1000 Hz
- // UIRxLowLimit[5] = m_dRxLowLimit[5]; // 3000 Hz
- // UIRxLowLimit[6] = m_dRxLowLimit[6]; // 3400 Hz
- // UIRxLowLimit[7] = m_dRxLowLimit[7]; // 4000 Hz
- for( i=0; i<TOTAL_TRC_FREQ_NUM; i++ )
- {
- OrgSpCoef.MagdB[i] = 0;
- }
- vDistance = 40;
- hDistance = 50;
- vScale = 10;
- hOutScale1 = 250;
- hOutScale2 = 150;
- hInScale1 = 20;
- hInScale2 = 50;
- vStart = 10;
- hStart = 100;
- hStart2 = hOutScale1*log10(100*9/hInScale1);
-
- PlotGrid(100, 4000, -20, 15);
- PlotBoundaryCurve();
- }
- //==============================================================================
- void __fastcall TfrmFIRCoeffCal::FormCreate(TObject *Sender)
- {
- cbBoundary->ItemIndex = 0;
- for (int i=0; i<TOTAL_SEL_FREQ_NUM; i++)
- {
- UISelectedSpCoef.Hz[i] = 0.0;
- UISelectedSpCoef.MagdB[i] = 0.0;
- }
- // taps
- edtFIRTaps->Text = (AnsiString) 30;
- m_iWantedTaps = 30;
- isWantedCircleOnDrag = false;
- WantedCircleOnDragIndex = -1;
- // 100, 200, 300, 500, 700, 1000, 2000, 3000, 3400, 3950
- // 100, 200, 300, 500, 600, 700, 800, 1000, 1300, 1800, 2000, 2500, 3000, 3400, 3950
- m_iWantedHz[0] = 100;
- m_iWantedHz[1] = 200;
- m_iWantedHz[2] = 300;
- m_iWantedHz[3] = 500;
- m_iWantedHz[4] = 600;
- m_iWantedHz[5] = 700;
- m_iWantedHz[6] = 800;
- m_iWantedHz[7] = 1000;
- m_iWantedHz[8] = 1300;
- m_iWantedHz[9] = 1800;
- m_iWantedHz[10] = 2000;
- m_iWantedHz[11] = 2500;
- m_iWantedHz[12] = 3000;
- m_iWantedHz[13] = 3400;
- m_iWantedHz[14] = 3900;
- m_bFirstFormShow = true;
- Init();
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::Init(void)
- {
- m_bInit = true;
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmFIRCoeffCal::btnLoadClick(TObject *Sender)
- {
- bool ok;
- if (rbAcqua->Checked)
- {
- ok = dlgOpenAcqua->Execute();
- }
- else if (rbMicrotronix->Checked)
- {
- ok = dlgOpenMicrotronix->Execute();
- }
- else if (rbUPL->Checked)
- {
- ok = dlgOpenUpl->Execute();
- }
- else if (m_rbUPV->Checked)
- {
- ok = m_dlgUPV->Execute();
- }
- else
- {
- ok = m_dlgOpenVita->Execute();
- }
- if( !ok )
- {
- sbAudioCal->Panels->Items[0]->Text = (AnsiString) " Load TRC file fail";
- Application->MessageBox( "Execution Failure : Load TRC file", "FAILURE", MB_OK );
- return;
- }
- if (rbAcqua->Checked)
- {
- ok = LoadAcquaFile (dlgOpenAcqua->FileName.c_str());
- }
- else if (rbMicrotronix->Checked)
- {
- ok = LoadMicrotronixFile(dlgOpenMicrotronix->FileName.c_str());
- }
- if (rbUPL->Checked)
- {
- ok = LoadTRCFile(dlgOpenUpl->FileName.c_str());
- }
- else if (m_rbUPV->Checked)
- {
- ok = LoadUpvFile(m_dlgUPV->FileName.c_str());
- }
- else
- {
- ok = LoadVitaFile(m_dlgOpenVita->FileName.c_str());
- }
- if (!ok)
- {
- sbAudioCal->Panels->Items[0]->Text = (AnsiString) " Load file fail";
- Application->MessageBox( "Execution Failure : Load file", "FAILURE", MB_OK );
- }
- }
- //---------------------------------------------------------------------------
-
- //---------------------------------------------------------------------------
- void __fastcall TfrmFIRCoeffCal::cbExtraFreqChange(TObject *Sender)
- {
- char str[256];
- if( cbExtraFreq->ItemIndex < 0) return;
- sprintf(str, "%.3f", WantedSpCoef.MagdB[cbExtraFreq->ItemIndex] );
- edtExtraDB->Text = (AnsiString) str;
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::ClearImage( void )
- {
- imAudioCal->Canvas->Brush->Color = clWhite;
- imAudioCal->Canvas->Brush->Style = bsSolid;
- imAudioCal->Canvas->FillRect(Rect(0,0,imAudioCal->Width,imAudioCal->Height));
- }
- //------------------------------------------------------------------------------
- void TfrmFIRCoeffCal::PlotBoundaryCurve( void )
- {
- int i;
- // float k = k=2+(20*log10(500)-20*log10(300))*(0-2)/(20*log10(1000)-20*log10(300));;
- imAudioCal->Canvas->Pen->Color = clLime;
- imAudioCal->Canvas->Pen->Width = 1;
- imAudioCal->Canvas->Pen->Style = psSolid;
- if( rbTx->Checked )
- {
- // draw upper limit
- // imAudioCal->Canvas->MoveTo( 1, vStart + vDistance*( (max_y_grid-UITxUpLimit[0])/vScale) );
- imAudioCal->Canvas->MoveTo( 1, vStart + vDistance*( (max_y_grid-m_dTxUpLimit[0])/vScale) );
- for( i=1; i<m_iTxLimitNum; i++ )
- {
- // if( UITxLimitFreq[i] <= 1000 )
- if( m_iTxLimitFreq[i] <= 1000 )
- {
- // if( log10(1.0*(UITxLimitFreq[i]-hStart)/hInScale1) > 0 )
- if( log10(1.0*(m_iTxLimitFreq[i]-hStart)/hInScale1) > 0 )
- {
- // imAudioCal->Canvas->LineTo( hOutScale1*log10(1.0*(UITxLimitFreq[i]-hStart)/hInScale1), vStart + vDistance*((max_y_grid-UITxUpLimit[i])/vScale) );
- imAudioCal->Canvas->LineTo( hOutScale1*log10(1.0*(m_iTxLimitFreq[i]-hStart)/hInScale1), vStart + vDistance*((max_y_grid-m_dTxUpLimit[i])/vScale) );
- }
- else
- {
- // imAudioCal->Canvas->LineTo( 1, vStart + vDistance*((max_y_grid-UITxUpLimit[i])/vScale) );
- imAudioCal->Canvas->LineTo( 1, vStart + vDistance*((max_y_grid-m_dTxUpLimit[i])/vScale) );
- }
- }
- else
- {
- //if( log10(1.0*(UITxLimitFreq[i]- 1000)/(hInScale2)) > 0 )
- if( log10(1.0*(m_iTxLimitFreq[i]- 1000)/(hInScale2)) > 0 )
- {
- //imAudioCal->Canvas->LineTo( hStart2 + hOutScale2*log10(1.0*(UITxLimitFreq[i]- 1000)/(hInScale2)) , vStart + vDistance*( (max_y_grid-UITxUpLimit[i])/vScale) );
- imAudioCal->Canvas->LineTo( hStart2 + hOutScale2*log10(1.0*(m_iTxLimitFreq[i]- 1000)/(hInScale2)) , vStart + vDistance*( (max_y_grid-m_dTxUpLimit[i])/vScale) );
- }
- else
- {
- // imAudioCal->Canvas->LineTo( hStart2, vStart + vDistance*( (max_y_grid-UITxUpLimit[i])/vScale) );
- imAudioCal->Canvas->LineTo( hStart2, vStart + vDistance*( (max_y_grid-m_dTxUpLimit[i])/vScale) );
- }
- }
- }
- // draw lower limit
- imAudioCal->Canvas->MoveTo( 1, vStart + vDistance*( (max_y_grid-m_dTxLowLimit[0])/vScale) );
- for( i=1; i<m_iTxLimitNum; i++ )
- {
- //if( UITxLimitFreq[i] <= 1000 )
- if( m_iTxLimitFreq[i] <= 1000 )
- {
- // if( log10(1.0*(UITxLimitFreq[i]-hStart)/hInScale1) > 0 )
- if( log10(1.0*(m_iTxLimitFreq[i]-hStart)/hInScale1) > 0 )
- {
- // imAudioCal->Canvas->LineTo( hOutScale1*log10(1.0*(UITxLimitFreq[i]-hStart)/hInScale1), vStart + vDistance*( (max_y_grid-UITxLowLimit[i])/vScale) );
- imAudioCal->Canvas->LineTo( hOutScale1*log10(1.0*(m_iTxLimitFreq[i]-hStart)/hInScale1), vStart + vDistance*( (max_y_grid-m_dTxLowLimit[i])/vScale) );
- }
- else
- {
- imAudioCal->Canvas->LineTo( 1, vStart + vDistance*( (max_y_grid-m_dTxLowLimit[i])/vScale) );
- }
- }
- else
- {
- // if( log10(1.0*(UITxLimitFreq[i]- 1000)/(hInScale2)) > 0 )
- if( log10(1.0*(m_iTxLimitFreq[i]- 1000)/(hInScale2)) > 0 )
- {
- // imAudioCal->Canvas->LineTo( hStart2 + hOutScale2*log10(1.0*(UITxLimitFreq[i]- 1000)/(hInScale2)) , vStart + vDistance*((max_y_grid-UITxLowLimit[i])/vScale) );
- imAudioCal->Canvas->LineTo( hStart2 + hOutScale2*log10(1.0*(m_iTxLimitFreq[i]- 1000)/(hInScale2)) , vStart + vDistance*((max_y_grid-m_dTxLowLimit[i])/vScale) );
- }
- else
- {
- // imAudioCal->Canvas->LineTo( hStart2, vStart + vDistance*((max_y_grid-UITxLowLimit[i])/vScale) );
- imAudioCal->Canvas->LineTo( hStart2, vStart + vDistance*((max_y_grid-m_dTxLowLimit[i])/vScale) );
- }
- }
- }
- }
- else // RX
- {
- // draw upper limit
- //imAudioCal->Canvas->MoveTo( 1, vStart + vDistance*( (max_y_grid-UIRxUpLimit[0])/vScale) );
- imAudioCal->Canvas->MoveTo( 1, vStart + vDistance*( (max_y_grid-m_dRxUpLimit[0])/vScale) );
- for( i=1; i<m_iRxLimitNum; i++ )
- {
- // if( UIRxLimitFreq[i] <= 1000 )
- if( m_iRxLimitFreq[i] <= 1000 )
- {
- if( log10(1.0*(m_iRxLimitFreq[i]-hStart)/hInScale1) > 0 )
- {
- //imAudioCal->Canvas->LineTo( hOutScale1*log10(1.0*(UIRxLimitFreq[i]-hStart)/hInScale1), vStart + vDistance*( (max_y_grid-UIRxUpLimit[i])/vScale) );
- imAudioCal->Canvas->LineTo( hOutScale1*log10(1.0*(m_iRxLimitFreq[i]-hStart)/hInScale1), vStart + vDistance*( (max_y_grid-m_dRxUpLimit[i])/vScale) );
- }
- else
- {
- // imAudioCal->Canvas->LineTo( 1, vStart + vDistance*( (max_y_grid-UIRxUpLimit[i])/vScale) );
- imAudioCal->Canvas->LineTo( 1, vStart + vDistance*( (max_y_grid-m_dRxUpLimit[i])/vScale) );
- }
- }
- else
- {
- // if( log10(1.0*(UIRxLimitFreq[i]- 1000)/hInScale2) > 0 )
- if( log10(1.0*(m_iRxLimitFreq[i]- 1000)/hInScale2) > 0 )
- {
- //imAudioCal->Canvas->LineTo( hStart2 + hOutScale2*log10(1.0*(UIRxLimitFreq[i]- 1000)/(hInScale2)) , vStart + vDistance*( (max_y_grid-UIRxUpLimit[i])/vScale) );
- imAudioCal->Canvas->LineTo( hStart2 + hOutScale2*log10(1.0*(m_iRxLimitFreq[i]- 1000)/(hInScale2)) , vStart + vDistance*( (max_y_grid-m_dRxUpLimit[i])/vScale) );
- }
- else
- {
- //imAudioCal->Canvas->LineTo( hStart2 , vStart + vDistance*( (max_y_grid-UIRxUpLimit[i])/vScale) );
- imAudioCal->Canvas->LineTo( hStart2 , vStart + vDistance*( (max_y_grid-m_dRxUpLimit[i])/vScale) );
- }
- }
- }
- // draw lower limit
- //imAudioCal->Canvas->MoveTo( 1, vStart + vDistance*( (max_y_grid-UIRxLowLimit[0])/vScale) );
- imAudioCal->Canvas->MoveTo( 1, vStart + vDistance*( (max_y_grid-m_dRxLowLimit[0])/vScale) );
- for( i=1; i<m_iRxLimitNum; i++ )
- {
- // if( UIRxLimitFreq[i] <= 1000 )
- if( m_iRxLimitFreq[i] <= 1000 )
- {
- // if( log10(1.0*(UIRxLimitFreq[i]-hStart)/hInScale1) > 0 )
- if( log10(1.0*(m_iRxLimitFreq[i]-hStart)/hInScale1) > 0 )
- {
- //imAudioCal->Canvas->LineTo( hOutScale1*log10(1.0*(UIRxLimitFreq[i]-hStart)/hInScale1), vStart + vDistance*( (max_y_grid-UIRxLowLimit[i])/vScale) );
- imAudioCal->Canvas->LineTo( hOutScale1*log10(1.0*(m_iRxLimitFreq[i]-hStart)/hInScale1), vStart + vDistance*( (max_y_grid-m_dRxLowLimit[i])/vScale) );
- }
- else
- {
- //imAudioCal->Canvas->LineTo( 1, vStart + vDistance*( (max_y_grid-UIRxLowLimit[i])/vScale) );
- imAudioCal->Canvas->LineTo( 1, vStart + vDistance*( (max_y_grid-m_dRxLowLimit[i])/vScale) );
- }
- }
- else
- {
- // if( log10(1.0*(UIRxLimitFreq[i]- 1000)/hInScale2) > 0 )
- if( log10(1.0*(m_iRxLimitFreq[i]- 1000)/hInScale2) > 0 )
- {
- // imAudioCal->Canvas->LineTo( hStart2 + hOutScale2*log10(1.0*(UIRxLimitFreq[i]- 1000)/(hInScale2)) , vStart + vDistance*( (max_y_grid-UIRxLowLimit[i])/vScale) );
- imAudioCal->Canvas->LineTo( hStart2 + hOutScale2*log10(1.0*(m_iRxLimitFreq[i]- 1000)/(hInScale2)) , vStart + vDistance*( (max_y_grid-m_dRxLowLimit[i])/vScale) );
- }
- else
- {
- // imAudioCal->Canvas->LineTo( hStart2 , vStart + vDistance*( (max_y_grid-UIRxLowLimit[i])/vScale) );
- imAudioCal->Canvas->LineTo( hStart2 , vStart + vDistance*( (max_y_grid-m_dRxLowLimit[i])/vScale) );
- }
- }
- }
- }
- }
- //==============================================================================
- void __fastcall TfrmFIRCoeffCal::rbTxClick(TObject *Sender)
- {
- double min_y, max_y;
- btnSetupOutputTxFile->Enabled = true;
- btnSetupOutpuRxFile->Enabled = false;
- stOutputTxFileName->Enabled = true;
- stOutputRxFileName->Enabled = false;
- GetMinMaxY( &OrgSpCoef, &min_y, &max_y );
- if( min_y == max_y || isTRCLoadOk == false || OrgSpCoef.num>TOTAL_TRC_FREQ_NUM)
- {
- PlotGrid(100, 4000, -20, 15);
- }
- else
- {
- PlotGrid( 100, 4000, min_y, max_y);
- }
- PlotBoundaryCurve();
- if( isOrgCurvePlot )
- {
- PlotOrgCurve();
- }
-
- sbAudioCal->Panels->Items[0]->Text = (AnsiString) " Change to TX";
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmFIRCoeffCal::rbRxClick(TObject *Sender)
- {
- double min_y, max_y;
- btnSetupOutputTxFile->Enabled = false;
- btnSetupOutpuRxFile->Enabled = true;
- stOutputTxFileName->Enabled = false;
- stOutputRxFileName->Enabled = true;
- GetMinMaxY( &OrgSpCoef, &min_y, &max_y );
- if( min_y == max_y || isTRCLoadOk == false || OrgSpCoef.num>TOTAL_TRC_FREQ_NUM)
- {
- PlotGrid(100, 4000, -20, 15);
- }
- else
- {
- PlotGrid( 100, 4000, min_y, max_y);
- }
- PlotBoundaryCurve();
- if( isOrgCurvePlot )
- {
- PlotOrgCurve();
- }
- sbAudioCal->Panels->Items[0]->Text = (AnsiString) " Change to RX";
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::IdealizeExcute(void)
- {
- //int i;
- int total_boundary_num;
- int *Af, Pyf[TOTAL_TRC_FREQ_NUM+1];
- int px, qx;
- double *AdB, Pyy[TOTAL_TRC_FREQ_NUM+1];
- double py, qy;
- // if( rbGoodSound->Checked )
- // {
- // total_boundary_num = 15;
- // }
- // else
- // {
- // total_boundary_num = 8;
- // }
- // getting bounday
- if (rbTx->Checked)
- {
- total_boundary_num = m_iTxLimitNum;
- for (int i=0; i<total_boundary_num; i++)
- {
- m_iHz_bound[i] = m_iTxLimitFreq[i];
- m_dLow_bound_dB[i] = m_dTxLowLimit[i];
- m_dUp_bound_dB[i]= m_dTxUpLimit[i];
- }
- m_dLow_limit_dB = -30;
- m_dHz100_limit_dB = -40;
- }
- else
- {
- total_boundary_num = m_iRxLimitNum;
- for (int i=0; i<total_boundary_num; i++)
- {
- m_iHz_bound[i] = m_iRxLimitFreq[i];
- m_dLow_bound_dB[i] = m_dRxLowLimit[i];
- m_dUp_bound_dB[i] = m_dRxUpLimit[i];
- }
- m_dLow_limit_dB = -20;
- m_dHz100_limit_dB = -30;
- }
- if ((ITEMIDX_BOUNDARY_SPEC == cbBoundary->ItemIndex) ||
- (ITEMIDX_BOUNDARY_3G_HANDSET_AND_HEADSET == cbBoundary->ItemIndex)
- )
- {
- for (int i=0; i<total_boundary_num; i++)
- {
- if (m_dLow_bound_dB[i] < -25)
- {
- m_dLow_bound_dB[i] = m_dLow_limit_dB;
- }
- }
- m_dLow_bound_dB[0] = -90 - m_dUp_bound_dB[0];
- m_dLow_bound_dB[7] = -90 - m_dUp_bound_dB[7];
- }
- if (
- (ITEMIDX_BOUNDARY_3G_HANDSFREE_DESKTOP_AND_VEHICLE == cbBoundary->ItemIndex) ||
- (ITEMIDX_BOUNDARY_3G_HANDSFREE_HANDHELD == cbBoundary->ItemIndex) ||
- (ITEMIDX_BOUNDARY_VOIP_HANDSET == cbBoundary->ItemIndex) ||
- (ITEMIDX_BOUNDARY_VOIP_HEADSET == cbBoundary->ItemIndex) ||
- (ITEMIDX_BOUNDARY_TYPE_3_3 == cbBoundary->ItemIndex)
- )
- {
- for (int i=0; i<total_boundary_num; i++)
- {
- if (m_dLow_bound_dB[i] < -25)
- {
- m_dLow_bound_dB[i] = m_dLow_limit_dB;
- }
- }
- }
- // getting mean boundary
- for (int i=0; i<total_boundary_num; i++)
- {
- m_dMean_bound_dB[i] = (m_dLow_bound_dB[i] + m_dUp_bound_dB[i]) * 0.5;
- }
- // Af = m_iHz_bound;
- // AdB = m_dMean_bound_dB;
- IdealSpCoef.Hz[0] = OrgSpCoef.Hz[0];
- IdealSpCoef.MagdB[0] = m_dMean_bound_dB[0]+(OrgSpCoef.Hz[0]-m_iHz_bound[0])*(m_dMean_bound_dB[1]-m_dMean_bound_dB[0])/(m_iHz_bound[1]-m_iHz_bound[0]);
- IdealSpCoef.Mag[0] = pow(10, IdealSpCoef.MagdB[0]/20.0);
- for (unsigned int i=1; i<OrgSpCoef.num; i++)
- {
- bool b_hit = false;
- IdealSpCoef.Hz[i] = OrgSpCoef.Hz[i];
- for (int j=1; j<total_boundary_num; j++)
- {
- if ((OrgSpCoef.Hz[i] > m_iHz_bound[j-1]) && (OrgSpCoef.Hz[i] <= m_iHz_bound[j]))
- {
- b_hit = true;
- px=m_iHz_bound[j-1];
- qx=m_iHz_bound[j];
- py=m_dMean_bound_dB[j-1];
- qy=m_dMean_bound_dB[j];
- }
- }
- if (b_hit)
- {
- IdealSpCoef.MagdB[i]=py+(qy-py)/(qx-px)*(OrgSpCoef.Hz[i]-px);
- //IdealSpCoef.Mag[i] = pow(10, IdealSpCoef.MagdB[i]/20.0);
- }
- else
- {
- IdealSpCoef.MagdB[i] = m_dMean_bound_dB[i]+(OrgSpCoef.Hz[i]-m_iHz_bound[i])*(m_dMean_bound_dB[i+1]-m_dMean_bound_dB[i])/(m_iHz_bound[i+1]-m_iHz_bound[i]);
- }
- if (IdealSpCoef.MagdB[i] < m_dLow_limit_dB)
- {
- IdealSpCoef.MagdB[i] = m_dLow_limit_dB;
- }
- IdealSpCoef.Mag[i] = pow(10, IdealSpCoef.MagdB[i]/20.0);
- }
- IdealSpCoef.num = OrgSpCoef.num;
- WantedSpCoef.num = OrgSpCoef.num;
- for (unsigned int i=0; i<WantedSpCoef.num; i++)
- {
- WantedSpCoef.Hz[i] = IdealSpCoef.Hz[i];
- WantedSpCoef.Mag[i] = IdealSpCoef.Mag[i];
- WantedSpCoef.MagdB[i] = IdealSpCoef.MagdB[i];
- }
- }
- //==============================================================================
- void TfrmFIRCoeffCal::PlotCurve(int plot_mask)
- {
- // plot grid
-
- if (plot_mask & PLOT_GRID)
- {
- double min_y, max_y;
- GetMinMaxY( &OrgSpCoef, &min_y, &max_y );
- if( min_y == max_y || isTRCLoadOk == false || OrgSpCoef.num>TOTAL_TRC_FREQ_NUM)
- {
- PlotGrid(100, 4000, -20, 15);
- }
- else
- {
- PlotGrid( 100, 4000, min_y, max_y);
- }
- }
- // plot boundary
- if (plot_mask & PLOT_BOUNDARY_CURVE)
- {
- Set_Boundary();
- PlotBoundaryCurve();
- }
- // plot wanted curve
- if( plot_mask & PLOT_WANTED_CURVE )
- {
- PlotWantedCurve();
- PlotWantedCircle();
- }
- // plot original curve
- if( plot_mask & PLOT_ORG_CURVE )
- {
- PlotOrgCurve();
- }
- // plot compensate curve
- if( plot_mask & PLOT_COMPENSATED_CURVE )
- {
- PlotCompensatedCurve();
- }
- }
- //------------------------------------------------------------------------
- void TfrmFIRCoeffCal::PlotGrid(int min_x, int max_x, int min_y, int max_y)
- {
- int i;
- double range;
- ClearImage();
- // get max_y_grid, min_y_grid;
- max_y_grid = getMaxYBound( max_y, 10 );
- min_y_grid = getMinYBound( min_y, 10 );
- if( min_y_grid < -20 )
- {
- min_y_grid = -20;
- }
- range = max_y_grid - min_y_grid;
- // recalculate vDistance
- // vDistance = imAudioCal->Height / (max_y - min_y) * 10;
- // vScale = (max_y - min_y)/8;
- for( i=1; i<=100; i++ )
- {
- vScale = i*VSCALE_UNIT;
- if( vScale * 8 > range)
- {
- break;
- }
- }
- vDistance = imAudioCal->Height / (range+1) * vScale;
- // vScale = range/8;
- // if( vScale > 5 )
- // {
- // vScale = 10;
- // }
- // else
- // {
- // vScale = 5;
- // }
- // if( (imAudioCal->Height % vDistance) != 0 )
- // {
- // Segments = imAudioCal->Height/vDistance + 1;
- // }
- // else
- // {
- // Segments = imAudioCal->Height/vDistance;
- //}
- // if( (range % vScale) != 0 )
- {
- Segments = (range / vScale) + 1;
- }
- // else
- // {
- // Segments = (range / vScale);
- // }
- // hDistance = 50;
- imAudioCal->Canvas->Pen->Color = clBlack;
- imAudioCal->Canvas->Pen->Width = 1;
- imAudioCal->Canvas->Pen->Style = psDot;
- // plot horizotal grid & label
- for( i=0; i<Segments && i<VERTICAL_LABEL_NUM; i++ )
- {
- if( (i*vDistance + vStart) > imAudioCal->Height )
- {
- break;
- }
- lblVertical[i]->Visible = true;
- lblVertical[i]->Top = imAudioCal->Top + i*vDistance - 5 + vStart;
- lblVertical[i]->Left = imAudioCal->Left - 20;
- lblVertical[i]->Caption = IntToStr( max_y_grid - i*vScale );
- imAudioCal->Canvas->MoveTo( 1, i*vDistance + vStart );
- imAudioCal->Canvas->LineTo( 1 + imAudioCal->Width, i*vDistance +vStart );
- }
- for(; i<VERTICAL_LABEL_NUM; i++)
- {
- lblVertical[i]->Visible = false;
- }
- // plot vertical grid
- lbl100Hz->Left = imAudioCal->Left - 10;
- lbl100Hz->Top = imAudioCal->Top + imAudioCal->Height + 5;
- lbl1000Hz->Left = imAudioCal->Left + hStart2 - 10;
- lbl1000Hz->Top = imAudioCal->Top + imAudioCal->Height + 5;
- for( i=1; i<=9; i++ )
- {
- if( log10(100.0*i/hInScale1) > 0 )
- {
- imAudioCal->Canvas->MoveTo( hOutScale1*log10(100.0*i/hInScale1), 1 );
- imAudioCal->Canvas->LineTo( hOutScale1*log10(100.0*i/hInScale1), imAudioCal->Height );
- }
- else
- {
- imAudioCal->Canvas->MoveTo( 1, 1 );
- imAudioCal->Canvas->LineTo( 1, imAudioCal->Height );
- }
- }
- for( i=1; i<4; i++ )
- {
- if( log10(i*1000.0/hInScale2) > 0 )
- {
- imAudioCal->Canvas->MoveTo( hStart2 + hOutScale2*log10(i*1000.0/(hInScale2)) , 1 );
- imAudioCal->Canvas->LineTo( hStart2 + hOutScale2*log10(i*1000.0/(hInScale2)), imAudioCal->Height );
- }
- else
- {
- imAudioCal->Canvas->MoveTo( hStart2, 1 );
- imAudioCal->Canvas->LineTo( hStart2, imAudioCal->Height );
- }
- }
- }
- //------------------------------------------------------------------------
- void TfrmFIRCoeffCal::GetMinMaxY( sSpCoefCalParameters *p_SpCoef, double *p_min_y, double *p_max_y )
- {
- *p_min_y = 1000000.0;
- *p_max_y = -1000000.0;
- for (unsigned int i=0; i<p_SpCoef->num; i++)
- {
- if( *(p_SpCoef->MagdB+i) < *p_min_y )
- {
- *p_min_y = *(p_SpCoef->MagdB+i);
- }
- if( *(p_SpCoef->MagdB+i) > *p_max_y )
- {
- *p_max_y = *(p_SpCoef->MagdB+i);
- }
- }
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::PlotOrgCurve( void )
- {
- isOrgCurvePlot = true;
- imAudioCal->Canvas->Pen->Color = clBlue;
- imAudioCal->Canvas->Pen->Width = 1;
- imAudioCal->Canvas->Pen->Style = psSolid;
- imAudioCal->Canvas->MoveTo( 1, vStart + vDistance*((max_y_grid-OrgSpCoef.MagdB[0])/vScale) );
- for (unsigned int i=0; i<OrgSpCoef.num; i++)
- {
- if( OrgSpCoef.Hz[i]<=100.0 )
- {
- }
- else if( OrgSpCoef.Hz[i]>100.0 && OrgSpCoef.Hz[i]<= 1000.0 )
- {
- if( log10((OrgSpCoef.Hz[i]-hStart)/hInScale1) > 0 )
- {
- imAudioCal->Canvas->LineTo( hOutScale1*log10((OrgSpCoef.Hz[i]-hStart)/hInScale1), vStart + vDistance*(( max_y_grid-OrgSpCoef.MagdB[i])/vScale) );
- }
- else
- {
- imAudioCal->Canvas->LineTo( 1, vStart + vDistance*(( max_y_grid-OrgSpCoef.MagdB[i])/vScale) );
- }
- }
- else
- {
- if( log10(1.0*(OrgSpCoef.Hz[i]- 1000)/hInScale2) > 0 )
- {
- imAudioCal->Canvas->LineTo( hStart2 + hOutScale2*log10(1.0*(OrgSpCoef.Hz[i]- 1000)/(hInScale2)) , vStart + vDistance*((max_y_grid-OrgSpCoef.MagdB[i])/vScale) );
- }
- else
- {
- imAudioCal->Canvas->LineTo( hStart2 ,vStart + vDistance*( (max_y_grid-OrgSpCoef.MagdB[i])/vScale) );
- }
- }
- }
-
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::PlotWantedCurve( void )
- {
- isWantedCurvePlot = true;
- imAudioCal->Canvas->Pen->Color = clRed;
- imAudioCal->Canvas->Pen->Width = 1;
- imAudioCal->Canvas->Pen->Style = psDot;
- WantedPoint.num = WantedSpCoef.num;
- WantedPoint.x[0] = 1;
- WantedPoint.y[0] = vStart + vDistance*((max_y_grid-WantedSpCoef.MagdB[0])/vScale);
- imAudioCal->Canvas->MoveTo( 1, vStart + vDistance*((max_y_grid-WantedSpCoef.MagdB[0])/vScale) );
- for (unsigned int i=0; i<WantedSpCoef.num; i++)
- {
- if( WantedSpCoef.Hz[i]<=100.0 )
- {
- }
- else if( WantedSpCoef.Hz[i]>100.0 && WantedSpCoef.Hz[i]<= 1000.0 )
- {
- if( log10((WantedSpCoef.Hz[i]-hStart)/hInScale1) > 0 )
- {
- WantedPoint.x[i] = hOutScale1*log10((WantedSpCoef.Hz[i]-hStart)/hInScale1);
- }
- else
- {
- WantedPoint.x[i] = 1;
- }
- WantedPoint.y[i] = vStart + vDistance*((max_y_grid-WantedSpCoef.MagdB[i])/vScale);
- imAudioCal->Canvas->LineTo(WantedPoint.x[i], WantedPoint.y[i] );
- }
- else
- {
- if( log10(1.0*(WantedSpCoef.Hz[i]- 1000)/hInScale2) > 0 )
- {
- WantedPoint.x[i] = hStart2 + hOutScale2*log10(1.0*(WantedSpCoef.Hz[i]- 1000)/(hInScale2));
- WantedPoint.y[i] = vStart + vDistance*( (max_y_grid-WantedSpCoef.MagdB[i])/vScale);
- imAudioCal->Canvas->LineTo( WantedPoint.x[i] , WantedPoint.y[i] );
- }
- else
- {
- WantedPoint.x[i] = hStart2;
- WantedPoint.y[i] = vStart + vDistance*( (max_y_grid-WantedSpCoef.MagdB[i])/vScale);
- imAudioCal->Canvas->LineTo( WantedPoint.x[i] , WantedPoint.y[i] );
- }
- }
- }
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::PlotCompensatedCurve( void )
- {
- isWantedCurvePlot = true;
- imAudioCal->Canvas->Pen->Color = clBlack;
- imAudioCal->Canvas->Pen->Width = 1;
- imAudioCal->Canvas->Pen->Style = psSolid;
- imAudioCal->Canvas->MoveTo( 1, vStart + vDistance*((max_y_grid-ck_NewSpCoef.MagdB[0])/vScale) );
- for (unsigned int i=0; i<ck_NewSpCoef.num; i++)
- {
- if( ck_NewSpCoef.Hz[i]<=100.0 )
- {
- }
- else if( ck_NewSpCoef.Hz[i]>100.0 && ck_NewSpCoef.Hz[i]<= 1000.0 )
- {
- if( log10((ck_NewSpCoef.Hz[i]-hStart)/hInScale1) > 0 )
- {
- imAudioCal->Canvas->LineTo( hOutScale1*log10((ck_NewSpCoef.Hz[i]-hStart)/hInScale1), vStart + vDistance*( (max_y_grid-ck_NewSpCoef.MagdB[i])/vScale) );
- }
- else
- {
- imAudioCal->Canvas->LineTo( 1, vStart + vDistance*( (max_y_grid-ck_NewSpCoef.MagdB[i])/vScale) );
- }
- }
- else
- {
- if( log10(1.0*(ck_NewSpCoef.Hz[i]- 1000)/hInScale2) > 0 )
- {
- imAudioCal->Canvas->LineTo( hStart2 + hOutScale2*log10(1.0*(ck_NewSpCoef.Hz[i]- 1000)/(hInScale2)) , vStart + vDistance*((max_y_grid-ck_NewSpCoef.MagdB[i])/vScale) );
- }
- else
- {
- imAudioCal->Canvas->LineTo( hStart2, vStart + vDistance*((max_y_grid-ck_NewSpCoef.MagdB[i])/vScale) );
- }
- }
- }
- }
- //------------------------------------------------------------------------------
- void TfrmFIRCoeffCal::PlotWantedCircle( void )
- {
- imAudioCal->Canvas->Brush->Color = clAqua;
- for(int i=0; i<WantedPoint.num; i++)
- {
- imAudioCal->Canvas->Ellipse(WantedPoint.x[i]-2, WantedPoint.y[i]-2, WantedPoint.x[i]+2, WantedPoint.y[i]+2);
- }
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::UpdatecbExtraFreq(sSpCoefCalParameters* p_SpCoef)
- {
- cbExtraFreq->Items->Clear();
- for (unsigned int i=0; i<p_SpCoef->num; i++)
- {
- cbExtraFreq->Items->Add( Double_To_AnsiString(p_SpCoef->Hz[i]) + " Hz" );
- }
- cbExtraFreq->ItemIndex = 0;
- edtExtraDB->Text = p_SpCoef->MagdB[cbExtraFreq->ItemIndex];
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::Updatecb1xxHz( sSpCoefCalParameters* p_SpCoef, int idx )
- {
- cb1xxHz->Items->Clear();
- //for(int i=0; i<p_SpCoef->num /15; i++ )
- for(int i=SelectedSpCoef.index[0]; i<SelectedSpCoef.index[1]; i++ )
- {
- cb1xxHz->Items->Add( Double_To_AnsiString(p_SpCoef->Hz[i]) + " Hz" );
- }
- cb1xxHz->ItemIndex = idx;
- edt1xxHzdB->Text = p_SpCoef->MagdB[ SelectedSpCoef.index[0] + idx ];
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::Updatecb2xxHz( sSpCoefCalParameters* p_SpCoef, int idx )
- {
- cb2xxHz->Items->Clear();
- // for(int i=p_SpCoef->num /15; i<2*p_SpCoef->num/15; i++ )
- for(int i=SelectedSpCoef.index[1]; i<SelectedSpCoef.index[2]; i++ )
- {
- cb2xxHz->Items->Add( Double_To_AnsiString(p_SpCoef->Hz[i]) + " Hz" );
- }
- cb2xxHz->ItemIndex = idx;
- edt2xxHzdB->Text = p_SpCoef->MagdB[ SelectedSpCoef.index[1] + idx ];
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::Updatecb3xxHz( sSpCoefCalParameters* p_SpCoef, int idx )
- {
- cb3xxHz->Items->Clear();
- //for(int i=2*p_SpCoef->num /15; i<3*p_SpCoef->num/15; i++ )
- for(int i=SelectedSpCoef.index[2]; i<SelectedSpCoef.index[3]; i++ )
- {
- cb3xxHz->Items->Add( Double_To_AnsiString(p_SpCoef->Hz[i]) + " Hz" );
- }
- cb3xxHz->ItemIndex = idx;
- edt3xxHzdB->Text = p_SpCoef->MagdB[ SelectedSpCoef.index[2] + idx ];
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::Updatecb5xxHz( sSpCoefCalParameters* p_SpCoef, int idx )
- {
- cb5xxHz->Items->Clear();
- //for(int i=3*p_SpCoef->num /15; i<4*p_SpCoef->num/15; i++ )
- for(int i=SelectedSpCoef.index[3]; i<SelectedSpCoef.index[4]; i++ )
- {
- cb5xxHz->Items->Add( Double_To_AnsiString(p_SpCoef->Hz[i]) + " Hz" );
- }
- cb5xxHz->ItemIndex = idx;
- edt5xxHzdB->Text = p_SpCoef->MagdB[ SelectedSpCoef.index[3] + idx ];
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::Updatecb6xxHz( sSpCoefCalParameters* p_SpCoef, int idx )
- {
- cb6xxHz->Items->Clear();
- //for(int i=4*p_SpCoef->num /15; i<5*p_SpCoef->num/15; i++ )
- for(int i=SelectedSpCoef.index[4]; i<SelectedSpCoef.index[5]; i++ )
- {
- cb6xxHz->Items->Add( Double_To_AnsiString(p_SpCoef->Hz[i]) + " Hz" );
- }
- cb6xxHz->ItemIndex = idx;
- edt6xxHzdB->Text = p_SpCoef->MagdB[ SelectedSpCoef.index[4] + idx ];
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::Updatecb7xxHz( sSpCoefCalParameters* p_SpCoef, int idx )
- {
- cb7xxHz->Items->Clear();
- // for(int i=5*p_SpCoef->num /15; i<6*p_SpCoef->num/15; i++ )
- for(int i=SelectedSpCoef.index[5]; i<SelectedSpCoef.index[6]; i++ )
- {
- cb7xxHz->Items->Add( Double_To_AnsiString(p_SpCoef->Hz[i]) + " Hz" );
- }
- cb7xxHz->ItemIndex = idx;
- edt7xxHzdB->Text = p_SpCoef->MagdB[ SelectedSpCoef.index[5] + idx ];
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::Updatecb8xxHz( sSpCoefCalParameters* p_SpCoef, int idx )
- {
- cb8xxHz->Items->Clear();
- // for(int i=6*p_SpCoef->num /15; i<7*p_SpCoef->num/15; i++ )
- for(int i=SelectedSpCoef.index[6]; i<SelectedSpCoef.index[7]; i++ )
- {
- cb8xxHz->Items->Add( Double_To_AnsiString(p_SpCoef->Hz[i]) + " Hz" );
- }
- cb8xxHz->ItemIndex = idx;
- edt8xxHzdB->Text = p_SpCoef->MagdB[ SelectedSpCoef.index[6] + idx ];
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::Updatecb1xxxHz( sSpCoefCalParameters* p_SpCoef, int idx )
- {
- cb1xxxHz->Items->Clear();
- //for(int i=7*p_SpCoef->num /15; i<8*p_SpCoef->num/15; i++ )
- for(int i=SelectedSpCoef.index[7]; i<SelectedSpCoef.index[8]; i++ )
- {
- cb1xxxHz->Items->Add( Double_To_AnsiString(p_SpCoef->Hz[i]) + " Hz" );
- }
- cb1xxxHz->ItemIndex = idx;
- edt1xxxHzdB->Text = p_SpCoef->MagdB[ SelectedSpCoef.index[7] + idx ];
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::Updatecb15xxHz( sSpCoefCalParameters* p_SpCoef, int idx )
- {
- cb13xxHz->Items->Clear();
- // for(int i=8*p_SpCoef->num /15; i<9*p_SpCoef->num/15; i++ )
- for(int i=SelectedSpCoef.index[8]; i<SelectedSpCoef.index[9]; i++ )
- {
- cb13xxHz->Items->Add( Double_To_AnsiString(p_SpCoef->Hz[i]) + " Hz" );
- }
- cb13xxHz->ItemIndex = idx;
- edt13xxHzdB->Text = p_SpCoef->MagdB[ SelectedSpCoef.index[8] + idx ];
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::Updatecb18xxHz( sSpCoefCalParameters* p_SpCoef, int idx )
- {
- cb18xxHz->Items->Clear();
- //for(int i=9*p_SpCoef->num /15; i<10*p_SpCoef->num/15; i++ )
- for(int i=SelectedSpCoef.index[9]; i<SelectedSpCoef.index[10]; i++ )
- {
- cb18xxHz->Items->Add( Double_To_AnsiString(p_SpCoef->Hz[i]) + " Hz" );
- }
- cb18xxHz->ItemIndex = idx;
- edt18xxHzdB->Text = p_SpCoef->MagdB[ SelectedSpCoef.index[9] + idx ];
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::Updatecb2xxxHz( sSpCoefCalParameters* p_SpCoef, int idx )
- {
- cb2xxxHz->Items->Clear();
- //for(int i=10*p_SpCoef->num /15; i<11*p_SpCoef->num/15; i++ )
- for(int i=SelectedSpCoef.index[10]; i<SelectedSpCoef.index[11]; i++ )
- {
- cb2xxxHz->Items->Add( Double_To_AnsiString(p_SpCoef->Hz[i]) + " Hz" );
- }
- cb2xxxHz->ItemIndex = idx;
- edt2xxxHzdB->Text = p_SpCoef->MagdB[ SelectedSpCoef.index[10] + idx ];
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::Updatecb25xxHz( sSpCoefCalParameters* p_SpCoef, int idx )
- {
- cb25xxHz->Items->Clear();
- //for(int i=11*p_SpCoef->num /15; i<12*p_SpCoef->num/15; i++ )
- for(int i=SelectedSpCoef.index[11]; i<SelectedSpCoef.index[12]; i++ )
- {
- cb25xxHz->Items->Add( Double_To_AnsiString(p_SpCoef->Hz[i]) + " Hz" );
- }
- cb25xxHz->ItemIndex = idx;
- edt25xxHzdB->Text = p_SpCoef->MagdB[ SelectedSpCoef.index[11] + idx ];
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::Updatecb3xxxHz( sSpCoefCalParameters* p_SpCoef, int idx )
- {
- cb3xxxHz->Items->Clear();
- //for(int i=12*p_SpCoef->num /15; i<13*p_SpCoef->num/15; i++ )
- for(int i=SelectedSpCoef.index[12]; i<SelectedSpCoef.index[13]; i++ )
- {
- cb3xxxHz->Items->Add( Double_To_AnsiString(p_SpCoef->Hz[i]) + " Hz" );
- }
- cb3xxxHz->ItemIndex = idx;
- edt3xxxHzdB->Text = p_SpCoef->MagdB[ SelectedSpCoef.index[12] + idx ];
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::Updatecb34xxHz( sSpCoefCalParameters* p_SpCoef, int idx )
- {
- cb34xxHz->Items->Clear();
- // for(int i=13*p_SpCoef->num /15; i<14*p_SpCoef->num/15; i++ )
- for(int i=SelectedSpCoef.index[13]; i<SelectedSpCoef.index[14]; i++ )
- {
- cb34xxHz->Items->Add( Double_To_AnsiString(p_SpCoef->Hz[i]) + " Hz" );
- }
- cb34xxHz->ItemIndex = idx;
- edt34xxHzdB->Text = p_SpCoef->MagdB[ SelectedSpCoef.index[13] + idx ];
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::Updatecb39xxHz( sSpCoefCalParameters* p_SpCoef, int idx )
- {
- cb39xxHz->Items->Clear();
- //for(int i=14*p_SpCoef->num /15; i<p_SpCoef->num; i++ )
- for (unsigned int i=SelectedSpCoef.index[14]; i<p_SpCoef->num; i++ )
- {
- cb39xxHz->Items->Add( Double_To_AnsiString(p_SpCoef->Hz[i]) + " Hz" );
- }
- cb39xxHz->ItemIndex = idx;
- edt39xxHzdB->Text = p_SpCoef->MagdB[ SelectedSpCoef.index[14] + idx ];
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::UpdateAllSelectParameters( void )
- {
- for( int i=0; i<TOTAL_SEL_FREQ_NUM; i++ )
- {
- SelectedSpCoef.MagdB[i] = WantedSpCoef.MagdB[SelectedSpCoef.index[i]];
- }
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::UpdateAllSelectEditor( void )
- {
- char str[256];
- for( int i=0; i<TOTAL_SEL_FREQ_NUM; i++ )
- {
- UISelectedSpCoef.MagdB[i] = SelectedSpCoef.MagdB[i];
- sprintf(str, "%.3f", SelectedSpCoef.MagdB[i]);
- edtHzdB[i]->Text = (AnsiString ) str;
- }
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::UpdateExtradBEditor( void )
- {
- char str[256];
- sprintf(str, "%.3f", WantedSpCoef.MagdB[cbExtraFreq->ItemIndex]);
- edtExtraDB->Text = (AnsiString) str;
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::UpdateEditor( void )
- {
- GetWantedIndex( &WantedSpCoef, &SelectedSpCoef );
- char str[256];
- for( int i=0; i<TOTAL_SEL_FREQ_NUM; i++ )
- {
- UISelectedSpCoef.Hz[i] = SelectedSpCoef.Hz[i];
- UISelectedSpCoef.MagdB[i] = SelectedSpCoef.MagdB[i];
- //lblHz[i]->Caption = Double_To_AnsiString( SelectedSpCoef.Hz[i] ) + " Hz";
- sprintf(str, "%.3f", SelectedSpCoef.MagdB[i]);
- edtHzdB[i]->Text = (AnsiString ) str;
- }
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::UpdateSelectedLabel( void )
- {
- for( int i=0; i<TOTAL_SEL_FREQ_NUM; i++ )
- {
- lblHz[i]->Caption = Double_To_AnsiString( SelectedSpCoef.Hz[i] ) + " Hz";
- }
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::GetWantedIndex( sSpCoefCalParameters *p_SpCoefCal, sSelectedParameters *p_SpSelected )
- {
-
- unsigned index;
- for( int j=0; j<TOTAL_SEL_FREQ_NUM; j++ )
- {
- GetIndexOfMostSimilar( m_iWantedHz[j], p_SpCoefCal, &index );
- p_SpSelected->index[j] = index;
- p_SpSelected->Hz[j] = p_SpCoefCal->Hz[index];
- p_SpSelected->MagdB[j] = p_SpCoefCal->MagdB[index];
- }
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::GetIndexOfMostSimilar( int wanted_Hz, sSpCoefCalParameters *p_SpCoefCal, unsigned int* p_index )
- {
- int distance;
- distance = abs( wanted_Hz - p_SpCoefCal->Hz[0] );
- *p_index = 0;
- for (unsigned int i=1; i<p_SpCoefCal->num; i++ )
- {
- if( distance > abs( wanted_Hz - p_SpCoefCal->Hz[i] ) )
- {
- *p_index = i;
- distance = abs( wanted_Hz - p_SpCoefCal->Hz[i] );
- }
- }
- }
- //==============================================================================
- void __fastcall TfrmFIRCoeffCal::btnIdealizeClick(TObject *Sender)
- {
- IdealizeExcute();
- //PlotWantedCurve();
- //PlotWantedCircle();
- PlotCurve( PLOT_GRID | PLOT_BOUNDARY_CURVE | PLOT_WANTED_CURVE | PLOT_ORG_CURVE );
- UpdateAllSelectParameters();
- UpdateExtradBEditor();
- UpdateAllSelectEditor();
- sbAudioCal->Panels->Items[0]->Text = (AnsiString) " Idealize successfully";
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmFIRCoeffCal::btnRunClick(TObject *Sender)
- {
- AFT_RESULT ret;
- int wanted_ord;
- //static bool isMfileDllLoaded = false;
- //sSpCoefCalParameters temp_OrgSpCoef, temp_WantedSpCoef, ProcessSpCoef, ProcessOddSpCoef, WithoutFirSpCoef, NewFirOddSpCoef, ck_NewFirSpCoef;
- sSpCoefCalParameters temp_OrgSpCoef, temp_WantedSpCoef, ProcessOddSpCoef, WithoutFirSpCoef, NewFirOddSpCoef, ck_NewFirSpCoef;
- sSpCoefCalParameters impulse_hw_FIR;
- // sFirCoefParameters old_FIR_norm, NewFirF1Coef, NewFirCoeffF, NewFirCoeffF_abs, NewFirCoeffF1, NewFirCoeffF_norm, ZeroFirCoeff;
- sFirCoefParameters NewFirF1Coef, NewFirCoeffF, NewFirCoeffF_abs, NewFirCoeffF1, NewFirCoeffF_norm, ZeroFirCoeff;
- sFirCoefParameters NewFirCoeffF_norm_fx;
- sWeightParameters Weight;
- //bool odd_flag;
- double d_max, d_gain;
- AnsiString as_Filename;
- double min_y, max_y;
- //static bool isFirstConnectMatlab = true;
- // static bool result = false;
- // check Matlab license.dat
- // if( !SP_COEF_CAL_Object.CheckLicenseDat() )
- // {
- // sbAudioCal->Panels->Items[0]->Text = (AnsiString) " Please put Matlab license.dat under the same folder as Meta2.exe";
- // Application->MessageBox( "Please put Matlab license.dat under the same folder as Meta2.exe" , "ERROR", MB_OK );
- // return;
- // }
- // connect with Matlab check
- // if( isFirstConnectMatlab )
- // {
- // isFirstConnectMatlab = false;
- // sbAudioCal->Panels->Items[0]->Text = (AnsiString) " Connect with Matlab progressing";
- // result = SP_COEF_CAL_Object.CheckEngineExist();
- //
- // }
- // if( result == false )
- // {
- // sbAudioCal->Panels->Items[0]->Text = (AnsiString) " Connect with Matlab fail";
- // Application->MessageBox( " Connect with Matlab fail" , "ERROR", MB_OK );
- // return;
- // }
- // if( isMfileDllLoaded == false )
- // {
- //
- // // if( DllMan->LoadMfilesDllFunctions( this->Handle ) == false )
- // {
- //
- // return;
- // }
- // isMfileDllLoaded = true;
- // }
- m_iWantedTaps = edtFIRTaps->Text.ToInt();
- wanted_ord=m_iWantedTaps-1;
- // original frequency response
- temp_OrgSpCoef.num = OrgSpCoef.num;
- for (unsigned int i=0; i<temp_OrgSpCoef.num; i++)
- {
- temp_OrgSpCoef.Hz[i] = OrgSpCoef.Hz[i];
- temp_OrgSpCoef.Mag[i] = OrgSpCoef.Mag[i];
- temp_OrgSpCoef.MagdB[i] = 20*log10(temp_OrgSpCoef.Mag[i]+LogFloor);
- }
- temp_WantedSpCoef.num = WantedSpCoef.num;
- for (unsigned int i=0; i<temp_WantedSpCoef.num; i++)
- {
- temp_WantedSpCoef.Hz[i] = WantedSpCoef.Hz[i];
- temp_WantedSpCoef.Mag[i] = WantedSpCoef.Mag[i];
- temp_WantedSpCoef.MagdB[i] = 20*log10(temp_WantedSpCoef.Mag[i]+LogFloor);
- }
- // processing
- ProcessSpCoef.num = temp_OrgSpCoef.num;
- for (unsigned int i=0; i<temp_OrgSpCoef.num; i++)
- {
- ProcessSpCoef.Hz[i] = temp_OrgSpCoef.Hz[i];
- }
- if( temp_OrgSpCoef.num%2 != 0)
- {
- // odd length
- for (int i= (int) ProcessSpCoef.num-1; i>=0; i--)
- {
- ProcessSpCoef.Hz[i+1] = ProcessSpCoef.Hz[i];
- temp_OrgSpCoef.MagdB[i+1] = temp_OrgSpCoef.MagdB[i];
- temp_WantedSpCoef.MagdB[i+1] = temp_WantedSpCoef.MagdB[i];
- }
- ProcessSpCoef.Hz[0] = 1;
- ProcessSpCoef.num++;
- temp_OrgSpCoef.num++;
- temp_WantedSpCoef.num++;
- //odd_flag = true;
- }
- // else
- // {
- // odd_flag = false;
- // }
- //if( rbTx->Checked )
- {
- // old_FIR.taps = Tx_old_FIR.taps;
- old_FIR_norm.taps = old_FIR.taps;
- for (unsigned int i=0; i<old_FIR.taps; i++)
- {
- old_FIR_norm.FirCoef[i] = old_FIR.FirCoef[i]/(1.0*FIR_COEFF_SCALE);
- }
- //ActiveMan->SetActiveFunction( ::Req_freqz );
- ret = SP_COEF_CAL_Object.freqz( m_b45TapSupport, &old_FIR_norm, &ProcessSpCoef, 8000.0, &OldFirSpCoef );
- if( ret )
- {
- char str[256];
- strcpy( str, SP_COEF_CAL_Object.Get_ErrorString( ret ) );
- Application->MessageBox( str, "Fail", MB_OK );
- return;
- }
- //p_MfileEvent->ResetEvent();
- //wait_result = p_MfileEvent->WaitFor( 5000 );
- //p_event = new TEvent();
- // ResetEvent( MfileEvent );
- //wait_result = WaitForMultipleObjects(
- // 1, // number of handles in array
- // &MfileEvent, // array of event handles
- // TRUE, // wait till all are signaled
- // 5000);
- //wait_result = WaitForSingleObject( MfileEvent, 5000 );
- //if( wait_result == 1 ) // timeout
- //{
- // sbAudioCal->Panels->Items[0]->Text = (AnsiString) " Connect with Matlab timeout";
- // Application->MessageBox( " Connect with Matlab timeout" , "ERROR", MB_OK );
- // return;
- //}
- }
- WithoutFirSpCoef.num = temp_OrgSpCoef.num;
- for (unsigned int i=0; i<WithoutFirSpCoef.num; i++)
- {
- WithoutFirSpCoef.MagdB[i] = temp_OrgSpCoef.MagdB[i] - OldFirSpCoef.MagdB[i];
- }
- impulse_hw_FIR.num = WithoutFirSpCoef.num;
- for (unsigned int i=0; i<impulse_hw_FIR.num; i++)
- {
- impulse_hw_FIR.Mag[i] = WithoutFirSpCoef.Mag[i];
- impulse_hw_FIR.MagdB[i] = WithoutFirSpCoef.MagdB[i];
- }
- NewFirSpCoef.num = WithoutFirSpCoef.num;
- for (unsigned int i=0; i<NewFirSpCoef.num; i++)
- {
- NewFirSpCoef.MagdB[i] = temp_WantedSpCoef.MagdB[i] - WithoutFirSpCoef.MagdB[i];
- NewFirSpCoef.Mag[i]=pow(10, NewFirSpCoef.MagdB[i]/20.0);
- }
- if( wanted_ord>0 )
- {
- ProcessOddSpCoef.num = ProcessSpCoef.num + 2;
- ProcessOddSpCoef.Hz[0] = 0;
- for (unsigned int i=0; i<ProcessSpCoef.num; i++)
- {
- ProcessOddSpCoef.Hz[i+1]=ProcessSpCoef.Hz[i];
- }
- ProcessOddSpCoef.Hz[ProcessOddSpCoef.num-1] = 4000;
- NewFirOddSpCoef.num = NewFirSpCoef.num + 2;
- NewFirOddSpCoef.Mag[0] = NewFirSpCoef.Mag[0];
- for (unsigned int i=0; i<NewFirOddSpCoef.num; i++)
- {
- NewFirOddSpCoef.Mag[i+1] = NewFirSpCoef.Mag[i];
- }
- NewFirOddSpCoef.Mag[NewFirOddSpCoef.num-1] = 0;
- // weight
- Weight.num = NewFirOddSpCoef.num / 2;
- for (unsigned int i=0; i<Weight.num; i++)
- {
- Weight.weight[i] = 1;
- }
-
- ret = SP_COEF_CAL_Object.firls( wanted_ord, &ProcessOddSpCoef, &NewFirOddSpCoef, 8000.0, &NewFirCoeffF1);
- if( ret )
- {
- char str[256];
- strcpy( str, SP_COEF_CAL_Object.Get_ErrorString( ret ) );
- Application->MessageBox( str, "Fail", MB_OK );
- return;
- }
- // SP_COEF_CAL_Object.firls4input( wanted_ord, &ProcessOddSpCoef, &NewFirOddSpCoef, &Weight, 8000.0, &NewFirCoeffF1);
-
- }
- else
- {
- NewFirCoeffF1.taps = 3;
- NewFirCoeffF1.FirCoef[0] = 1;
- NewFirCoeffF1.FirCoef[1] = 0;
- NewFirCoeffF1.FirCoef[2] = 0;
- m_iWantedTaps=3;
- wanted_ord=m_iWantedTaps-1;
- Application->MessageBox( "The wanted order is less than or equal to 0. Force the wanted order to 3." , "Warnning", MB_OK );
- }
- NewFirCoeffF.taps = NewFirCoeffF1.taps;
- for (unsigned int i=0; i<NewFirCoeffF.taps; i++)
- {
- NewFirCoeffF.FirCoef[i] = NewFirCoeffF1.FirCoef[i];
- }
- if (m_b45TapSupport)
- { ZeroFirCoeff.taps = SPEECH_FIR_44_TAPS_NUM;
- }
- else
- {
- ZeroFirCoeff.taps = SPEECH_FIR_30_TAPS_NUM;
- }
- for (unsigned int i=0; i<ZeroFirCoeff.taps; i++)
- {
- ZeroFirCoeff.FirCoef[i] = 0.0;
- }
- AbsOfComplexArray( NewFirCoeffF.FirCoef, ZeroFirCoeff.FirCoef, NewFirCoeffF.taps, NewFirCoeffF_abs.FirCoef );
- NewFirCoeffF_abs.taps = NewFirCoeffF.taps;
- MaxOfDoubleArray( NewFirCoeffF_abs.FirCoef, NewFirCoeffF_abs.taps, &d_max );
- if( d_max == 0.0 ) d_max = 1.0;
- d_gain = 1.0 / d_max;
- NewFirCoeffF_norm.taps = wanted_ord+1;
- for (unsigned int i=0; i<NewFirCoeffF_norm.taps; i++)
- {
- NewFirCoeffF_norm.FirCoef[i] = NewFirCoeffF.FirCoef[i]*d_gain;
- }
- // check frequency
- ret = SP_COEF_CAL_Object.freqz( m_b45TapSupport, &NewFirCoeffF, &ProcessSpCoef, 8000.0, &ck_NewFirSpCoef );
- if( ret )
- {
- char str[256];
- strcpy( str, SP_COEF_CAL_Object.Get_ErrorString( ret ) );
- Application->MessageBox( str, "Fail", MB_OK );
- return;
- }
-
- ck_NewSpCoef.num = ck_NewFirSpCoef.num;
- for (unsigned int i=0; i<ck_NewSpCoef.num; i++)
- {
- ck_NewSpCoef.MagdB[i] = ck_NewFirSpCoef.MagdB[i] + impulse_hw_FIR.MagdB[i];
- ck_NewSpCoef.Hz[i] = ProcessSpCoef.Hz[i];
- }
- // Write output data
- // write data to output
- // Output coeffs. in floating-point format ==>new_fir_coeff_F_normalized
- // with length ==> new_fir_coeff_F_normalized_taps
- // Re-ordering old FIR coeffs. from [B(0), ..., B(N-1), B(N)] to [B(N), B(N-1), ..., B(0)]
- NewFirCoeffF_norm_fx.taps = NewFirCoeffF_norm.taps;
- for (unsigned int i=0; i<NewFirCoeffF_norm_fx.taps; i++)
- {
- NewFirCoeffF_norm_fx.FirCoef[i] = Round_double( FIR_COEFF_SCALE * NewFirCoeffF_norm.FirCoef[i]*FirScale );
- }
- SP_COEF_CAL_Object.ReorderFirCoef( &NewFirCoeffF_norm_fx );
- // mTimer->Enabled = true;
- // SP_COEF_CAL_Object.freqz2out( sFirCoefParameters *p_new_fir_coeff_F_normalized_fixed_point, double N, double freqz_N, double Fs, sFirRespParameters *p_FirResp_freqz_H, sFirRespParameters *p_FirResp_freqz_F );
- // mTimer->Enabled = false;
- // write FIR coefficient to file
- if( rbTx->Checked )
- {
- as_Filename = stOutputTxFileName->Caption;
- }
- else
- {
- as_Filename = stOutputRxFileName->Caption;
- }
- // as_Filename = getFullPathFileName( as_Filename );
- SP_COEF_CAL_Object.REQ_Write_FIR_File( as_Filename.c_str(), &NewFirCoeffF_norm_fx );
- // plot curve
- GetMinMaxY( &OrgSpCoef, &min_y, &max_y );
- if( min_y == max_y || isTRCLoadOk == false || OrgSpCoef.num>TOTAL_TRC_FREQ_NUM)
- {
- PlotGrid(100, 4000, -20, 15);
- }
- else
- {
- PlotGrid( 100, 4000, min_y, max_y);
- }
- PlotBoundaryCurve();
- PlotOrgCurve();
- PlotWantedCurve();
- PlotWantedCircle();
- PlotCompensatedCurve();
- // plot frequency response
- // mTimer->Enabled = true;
- // SP_COEF_CAL_Object.freqz2out( &NewFirCoeffF_norm_fx, 1.0, 1024.0, 8000.0, &FirResp_freqz_H, &FirResp_freqz_F );
- // mTimer->Enabled = false;
- //frmFirResponse->Top = 104;//frmRFTool->Top + (frmRFTool->Height-frmApcProfile->Height)/2;
- //frmFirResponse->Left = 42;//frmRFTool->Left + (frmRFTool->Width -frmApcProfile->Width)/2;
- //frmFirResponse->Show();
- sbAudioCal->Panels->Items[0]->Text = (AnsiString) " Run successfully";
- //frmFirResponse->imFirResponseExp->Canvas->Ellipse(50,50, 70, 70);
- }
- //---------------------------------------------------------------------------
- void TfrmFIRCoeffCal::Update_dB(sSelectedParameters *p_SelPar, int index, double dB, sSpCoefCalParameters *p_UPL_database)
- {
- int dB_now_idx=0;
- int dB_num, max_dB_num=TOTAL_SEL_FREQ_NUM-1, min_dB_num=0;
- // int Anum, Bnum;
- sInterpolateParameters S;
- // double Pyy[4], Pyf[4];//Org_Hz[4];
- double px, qx, py, qy;
- double min_y, max_y;
- sModifiedParameters Mod;
- // read database and write to dB editor using dB_idx
- //update UPL database
- // dB_num_len = 0;
- for (unsigned int i=0; i<p_UPL_database->num; i++)
- {
- if( p_UPL_database->Hz[i] == p_SelPar->Hz[index] )
- //if( UPL_database.Hz[i] == p_SpCoefCal->Hz[index] )
- {
- dB_now_idx = i;
- break;
- }
- }
- dB_num = index;
- p_UPL_database->Mag[dB_now_idx] = pow(10.0, dB/20.0);
- for (unsigned int i=0; i<p_UPL_database->num; i++)
- {
- p_UPL_database->MagdB[i] = 20.0*log10(p_UPL_database->Mag[i]+LogFloor);
- }
- // read all dB
- // interpolation
- if(dB_num==min_dB_num)
- {
- S.Anum=p_SelPar->index[min_dB_num];
- S.Bnum=p_SelPar->index[min_dB_num+1];
- S.num = 2;
- S.Hz[0] = p_UPL_database->Hz[S.Anum];
- S.Hz[1] = p_UPL_database->Hz[S.Bnum];
- S.MagdB[0] = p_UPL_database->MagdB[S.Anum];
- S.MagdB[1] = p_UPL_database->MagdB[S.Bnum];
- S.distance=S.Bnum-S.Anum+1;
- S.Start_data_idx=S.Anum;
- S.End_data_idx=S.Bnum;
- }
- else if(dB_num==max_dB_num)
- {
- S.Anum=p_SelPar->index[max_dB_num-1];
- S.Bnum=p_SelPar->index[max_dB_num];
- S.num = 2;
- S.Hz[0] = p_UPL_database->Hz[S.Anum];
- S.Hz[1] = p_UPL_database->Hz[S.Bnum];
- S.MagdB[0] = p_UPL_database->MagdB[S.Anum];
- S.MagdB[1] = p_UPL_database->MagdB[S.Bnum];
- S.distance=S.Bnum-S.Anum+1;
- S.Start_data_idx=S.Anum;
- S.End_data_idx=S.Bnum;
- }
- else
- {
- S.Anum = p_SelPar->index[dB_num-1];
- S.Bnum = p_SelPar->index[dB_num];
- S.Cnum = p_SelPar->index[dB_num+1];
- S.num = 3;
- S.Hz[0] = p_UPL_database->Hz[S.Anum];
- S.Hz[1] = p_UPL_database->Hz[S.Bnum];
- S.Hz[2] = p_UPL_database->Hz[S.Cnum];
- S.MagdB[0] = p_UPL_database->MagdB[S.Anum];
- S.MagdB[1] = p_UPL_database->MagdB[S.Bnum];
- S.MagdB[2] = p_UPL_database->MagdB[S.Cnum];
- S.distance=S.Cnum-S.Anum+1;
- S.Start_data_idx=S.Anum;
- S.End_data_idx=S.Cnum;
- }
- Mod.num=S.End_data_idx-S.Start_data_idx+1;
- for (int i=0; i<Mod.num; i++)
- {
- Mod.Pyy[i] = 0.0;
- Mod.Pyf[i] = 0.0;
- }
- for (int i=S.Start_data_idx; i<=S.End_data_idx; i++)
- {
- Mod.Hz[i-S.Start_data_idx] = p_UPL_database->Hz[i];
- }
- Mod.Pyf[0] = Mod.Hz[0];
- Mod.Pyy[0] = S.MagdB[0]+(Mod.Hz[0]-S.Hz[0])*(S.MagdB[1]-S.MagdB[0])/(S.Hz[1]-S.Hz[0]);
- for (int i=1; i<S.distance; i++)
- {
- double a=Mod.Hz[i];
- Mod.Pyf[i]=a;
- for(int j=1; j<S.num; j++)
- {
- if(a>S.Hz[j-1] && a<=S.Hz[j])
- {
- px=S.Hz[j-1];
- qx=S.Hz[j];
- py=S.MagdB[j-1];
- qy=S.MagdB[j];
- }
- }
- Mod.Pyy[i]=py+(qy-py)/(qx-px)*(a-px);
- }
- // Update UPL_database
- for (int i=S.Start_data_idx; i<=S.End_data_idx; i++)
- {
- p_UPL_database->MagdB[i]= Mod.Pyy[i-S.Start_data_idx];
- }
- for (unsigned int i=0; i<p_UPL_database->num; i++)
- {
- p_UPL_database->Mag[i]=pow(10.0, p_UPL_database->MagdB[i]/20.0);
- }
- // update orginal data
- OrgSpCoef.num = p_UPL_database->num;
- for (unsigned int i=0; i<OrgSpCoef.num; i++)
- {
- OrgSpCoef.MagdB[i] = 20.0*log10(OrgSpCoef.Mag[i]+LogFloor);
- }
- // update wanted data
- WantedSpCoef.num = p_UPL_database->num;
- for (unsigned int i=0; i<WantedSpCoef.num; i++)
- {
- WantedSpCoef.Mag[i] = p_UPL_database->Mag[i];
- WantedSpCoef.MagdB[i] = 20*log10(WantedSpCoef.Mag[i]+LogFloor);
- }
- // plot curve
- GetMinMaxY( &OrgSpCoef, &min_y, &max_y );
- if( min_y == max_y || isTRCLoadOk == false || OrgSpCoef.num>TOTAL_TRC_FREQ_NUM)
- {
- PlotGrid(100, 4000, -20, 15);
- }
- else
- {
- PlotGrid( 100, 4000, min_y, max_y);
- }
- PlotBoundaryCurve();
- PlotOrgCurve();
- PlotWantedCurve();
- PlotWantedCircle();
- }
- //==============================================================================
- bool TfrmFIRCoeffCal::edtdBCheck(TObject *Sender)
- {
- double data;
- AnsiString text;
- TEdit *edit = (TEdit*)Sender;
- char hint[] = " character is not valid ";
- text = edit->Text;
- if( !IsValidMagdB( text, data ) )
- {
- ShowHintLabel( edit, hint );
- edit->SetFocus();
- return false;
- }
- return true;
- }
- //-------------------------------------------------------------------------
- void __fastcall TfrmFIRCoeffCal::edt1xxHzdBExit(TObject *Sender)
- {
- double dB;
- if( ! edtdBCheck(Sender) )
- {
- return;
- }
-
- dB = atof(edt1xxHzdB->Text.c_str());
- if( dB != UISelectedSpCoef.MagdB[0] )
- {
- UISelectedSpCoef.MagdB[0] = dB;
- Update_dB(&SelectedSpCoef, 0, dB, &WantedSpCoef);
- Update_edtExtraDB( 0, dB );
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmFIRCoeffCal::edt2xxHzdBExit(TObject *Sender)
- {
- double dB;
- if( ! edtdBCheck(Sender) )
- {
- return;
- }
- dB = atof(edt2xxHzdB->Text.c_str());
-
- if( dB != UISelectedSpCoef.MagdB[1] )
- {
- UISelectedSpCoef.MagdB[1] = dB;
- Update_dB(&SelectedSpCoef, 1, dB, &WantedSpCoef);
- Update_edtExtraDB( 1, dB );
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmFIRCoeffCal::edt3xxHzdBExit(TObject *Sender)
- {
- double dB;
- if( ! edtdBCheck(Sender) )
- {
- return;
- }
- dB = atof(edt3xxHzdB->Text.c_str());
- if( dB != UISelectedSpCoef.MagdB[2] )
- {
- UISelectedSpCoef.MagdB[2] = dB;
- Update_dB(&SelectedSpCoef, 2, dB, &WantedSpCoef);
- Update_edtExtraDB( 2, dB );
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmFIRCoeffCal::edt5xxHzdBExit(TObject *Sender)
- {
- double dB;
- if( ! edtdBCheck(Sender) )
- {
- return;
- }
- dB = atof(edt5xxHzdB->Text.c_str());
-
- if( dB != UISelectedSpCoef.MagdB[3] )
- {
- UISelectedSpCoef.MagdB[3] = dB;
- Update_dB(&SelectedSpCoef, 3, dB, &WantedSpCoef);
- Update_edtExtraDB( 3, dB );
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmFIRCoeffCal::edt6xxHzdBExit(TObject *Sender)
- {
- double dB;
- if( ! edtdBCheck(Sender) )
- {
- return;
- }
- dB = atof(edt6xxHzdB->Text.c_str());
-
- if( dB != UISelectedSpCoef.MagdB[4] )
- {
- UISelectedSpCoef.MagdB[4] = dB;
- Update_dB(&SelectedSpCoef, 4, dB, &WantedSpCoef);
- Update_edtExtraDB( 4, dB );
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmFIRCoeffCal::edt7xxHzdBExit(TObject *Sender)
- {
- double dB;
- if( ! edtdBCheck(Sender) )
- {
- return;
- }
- dB = atof(edt7xxHzdB->Text.c_str());
-
- if( dB != UISelectedSpCoef.MagdB[5] )
- {
- UISelectedSpCoef.MagdB[5] = dB;
- Update_dB(&SelectedSpCoef, 5, dB, &WantedSpCoef);
- Update_edtExtraDB( 5, dB );
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmFIRCoeffCal::edt8xxHzdBExit(TObject *Sender)
- {
- double dB;
- if( ! edtdBCheck(Sender) )
- {
- return;
- }
- dB = atof(edt8xxHzdB->Text.c_str());
-
- if( dB != UISelectedSpCoef.MagdB[6] )
- {
- UISelectedSpCoef.MagdB[6] = dB;
- Update_dB(&SelectedSpCoef, 6, dB, &WantedSpCoef);
- Update_edtExtraDB( 6, dB );
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmFIRCoeffCal::edt1xxxHzdBExit(TObject *Sender)
- {
- double dB;
- if( ! edtdBCheck(Sender) )
- {
- return;
- }
- dB = atof(edt1xxxHzdB->Text.c_str());
-
- if( dB != UISelectedSpCoef.MagdB[7] )
- {
- UISelectedSpCoef.MagdB[7] = dB;
- Update_dB(&SelectedSpCoef, 7, dB, &WantedSpCoef);
- Update_edtExtraDB( 7, dB );
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmFIRCoeffCal::edt13xxHzdBExit(TObject *Sender)
- {
- double dB;
- if( ! edtdBCheck(Sender) )
- {
- return;
- }
- dB = atof(edt13xxHzdB->Text.c_str());
-
- if( dB != UISelectedSpCoef.MagdB[8] )
- {
- UISelectedSpCoef.MagdB[8] = dB;
- Update_dB(&SelectedSpCoef, 8, dB, &WantedSpCoef);
- Update_edtExtraDB( 8, dB );
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmFIRCoeffCal::edt18xxHzdBExit(TObject *Sender)
- {
- double dB;
- if( ! edtdBCheck(Sender) )
- {
- return;
- }
- dB = atof(edt18xxHzdB->Text.c_str());
-
- if( dB != UISelectedSpCoef.MagdB[9] )
- {
- UISelectedSpCoef.MagdB[9] = dB;
- Update_dB(&SelectedSpCoef, 9, dB, &WantedSpCoef);
- Update_edtExtraDB( 9, dB );
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmFIRCoeffCal::edt2xxxHzdBExit(TObject *Sender)
- {
- double dB;
- if( ! edtdBCheck(Sender) )
- {
- return;
- }
- dB = atof(edt2xxxHzdB->Text.c_str());
-
- if( dB != UISelectedSpCoef.MagdB[10] )
- {
- UISelectedSpCoef.MagdB[10] = dB;
- Update_dB(&SelectedSpCoef, 10, dB, &WantedSpCoef);
- Update_edtExtraDB( 10, dB );
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmFIRCoeffCal::edt25xxHzdBExit(TObject *Sender)
- {
- double dB;
- if( ! edtdBCheck(Sender) )
- {
- return;
- }
- dB = atof(edt25xxHzdB->Text.c_str());
-
- if( dB != UISelectedSpCoef.MagdB[11] )
- {
- UISelectedSpCoef.MagdB[11] = dB;
- Update_dB(&SelectedSpCoef, 11, dB, &WantedSpCoef);
- Update_edtExtraDB( 11, dB );
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmFIRCoeffCal::edt3xxxHzdBExit(TObject *Sender)
- {
- double dB;
- if( ! edtdBCheck(Sender) )
- {
- return;
- }
- dB = atof(edt3xxxHzdB->Text.c_str());
- if( dB != UISelectedSpCoef.MagdB[12] )
- {
- UISelectedSpCoef.MagdB[12] = dB;
- Update_dB(&SelectedSpCoef, 12, dB, &WantedSpCoef);
- Update_edtExtraDB( 12, dB );
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmFIRCoeffCal::edt34xxHzdBExit(TObject *Sender)
- {
- double dB;
- if( ! edtdBCheck(Sender) )
- {
- return;
- }
- dB = atof(edt34xxHzdB->Text.c_str());
-
- if( dB != UISelectedSpCoef.MagdB[13] )
- {
- UISelectedSpCoef.MagdB[13] = dB;
- Update_dB(&SelectedSpCoef, 13, dB, &WantedSpCoef);
- Update_edtExtraDB( 13, dB );
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmFIRCoeffCal::edt39xxHzdBExit(TObject *Sender)
- {
- double dB;