⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lpf.cpp

📁 这个我写的一个计算LC滤波器的程序
💻 CPP
字号:
// LPF.cpp: implementation of the CLPF class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Fileter.h"

#include "Compnent.h"
#include "LPF.h"
#include "math.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CLPF::CLPF()
{
	PassFreq=0;
	EffectFreq=0;
	PassVal=0;
	EffectVal=0;//As Am
	RL=0;
	////
	SectionNum=5;
	pCCompnent = new CCCompnent[SectionNum];
	pLCompnent = new CLCompnent[SectionNum];
}
CLPF::~CLPF()
{
	delete [] pCCompnent;
	delete [] pLCompnent;
}
CLPF::CLPF(double rl,double Pf,double Ef,double PVal,double EVal)
{
	RL=rl;
	PassFreq=Pf;
	EffectFreq=Ef;
	PassVal=PVal;
	EffectVal=EVal;//As Am
	SectionNum=5;
	pCCompnent = new CCCompnent[SectionNum];
	pLCompnent = new CLCompnent[SectionNum];
}
void CLPF::SetRL(double rl) //设置 阻抗
{
	RL=rl;
}
void CLPF::SetFrequence(double PassF,double EffectF) //设置 频率
{
	PassFreq=PassF;
	EffectFreq=EffectF;
}

void CLPF::SetNeed(double rl,double Pf,double Ef,double PVal,double EVal)
{
	RL=rl;
	PassFreq=Pf;
	EffectFreq=Ef;
	PassVal=PVal;
	EffectVal=EVal;//As Am
}

void CLPF::GetNeed(double &rl,double &Pf,double &Ef,double &PVal,double &EVal)
{
	rl=RL;
	Pf=PassFreq;
	Ef=EffectFreq;
	PVal=PassVal;
	EVal=EffectVal;//As Am
}

void CLPF::SetSectionNum(int sectionnum) //设置 节数N
{
	SectionNum=sectionnum;
}
int  CLPF::GetSectionNum(void) //得到 节数N
{
	return SectionNum;
}

double CLPF::GetAmpOut(double freq) //不同频率下的幅度
{//C1 L2 C3 L4 C5 L6 C7    R2 R4 R6
    CComplex_num IL, UL,PL;
    double dB;
	GetOut(UL,IL,freq);
	PL=UL * IL;
	dB=10*log10(PL.GetAmp());
    return (dB);
}
double CLPF::GetPhaseOut(double freq) //不同频率下的相位
{//C1 L2 C3 L4 C5 L6 C7    R2 R4 R6
    CComplex_num IL, UL,PL;
    double Phase;
	GetOut(UL,IL,freq);
	PL=UL * IL;
	Phase=10*log10(PL.GetPhase());
    return (Phase);
}
///////////////////////////////////////////////////////////////
//the Butter LPF
double CButterLPF::ButterData[7][7]={
	{1},
	{1.4142,1.4142},
	{1.0,2.0,1.0},
	{0.7654,1.8478,1.8478,0.7654},
	{0.6180,1.6180,2.0,1.6180,0.6180},
	{0.5176,1.4142,1.9319,1.9319,1.4142,0.5176},
	{0.4450,1.247,1.8019,2.0,1.8019,1.247,0.4450}};
CButterLPF::CButterLPF():CLPF()
{
	SectionNum=2;
	pCCompnent=NULL;
	pLCompnent=NULL;
}
CButterLPF::~CButterLPF()
{
//	delete pCCompnent;
//	delete pLCompnent;
}
CButterLPF::CButterLPF(int sectionnum):CLPF()
{
	SectionNum=sectionnum;
}

void CButterLPF::JudgeNeedNum(void)//利用已知条件得到 节数N
{//未完
	SectionNum=5;
}
void CButterLPF::SetVal(CCCompnent *pCCompnentIn,
						CLCompnent *pLCompnentIn)
{
	for(int i=0;i<SectionNum;i++)
	{
		if(i%2!=0)	
		{
			pLCompnent[i/2] =pLCompnentIn[i/2];  // is a L
			pCCompnent[i]= CCCompnent(0);
		}
		else pCCompnent[i]=pCCompnentIn[i]; // is a C
	}
}

void CButterLPF::GetVal(CCCompnent *pCCompnentOut,
				  CLCompnent *pLCompnentOut)
{
	for(int i=0;i<SectionNum;i++)
	{
		if(i%2!=0)	
		{
			pLCompnentOut[i/2] =pLCompnent[i/2]; // is L
			pCCompnentOut[i]= CCCompnent(0);
		}
		else pCCompnentOut[i] =pCCompnent[i];  // is a C
	}
}

void CButterLPF::CountValue()  //根据节数,查表得出元件
{//C1 L2 C3 L4 C5 L6 C7    R2 R4 R6
//INITTION THE COMPNETN
	delete [] pCCompnent;
	delete [] pLCompnent;
	pCCompnent = new CCCompnent[SectionNum];//[(SectionNum+1)/2];
	pLCompnent = new CLCompnent[SectionNum];//[SectionNum/2];

	double Omg=2*PI*PassFreq;
	double C=1.0/(Omg*RL);
	double L=RL/Omg;
	int i;
	for(i=0;i<SectionNum;i++)
	{
		if(i%2!=0) 
		{
			CLCompnent *pL=new CLCompnent(L*ButterData[SectionNum-1][i]);
		    pLCompnent[i/2]	=*pL;
			delete pL;
			pCCompnent[i] = CCCompnent(0);
		}
		else
		{
			CCCompnent *pC=new CCCompnent(C*ButterData[SectionNum-1][i]);
			pCCompnent[i]=*pC;
	        delete pC;
		}
	}
}

void CButterLPF::GetOut(CComplex_num &UL,CComplex_num &IL,double freq) //不同频率下的复数值大小
{//C1 L2 C3 L4 C5 L6 C7    R2 R4 R6
	int i;
    CComplex_num AllRL(RL,0), Is(1,0), Us(1,0);
	for(i=SectionNum-1;i>=0;i--)
	{
		if(i%2!=0) //is a L Compnent
		{
			CComplex_num L1=pLCompnent[i/2].GetComplexVal(freq);
			Us=(L1+AllRL)/AllRL *Us; //UL is 1
			AllRL=AllRL+L1;
		}
		else  //is a C Compnent
		{
			CComplex_num C1=pCCompnent[i].GetComplexVal(freq);
			Is=(C1+AllRL)/C1  *Is; //IL is 1
			AllRL=AllRL%C1;
		}
	}
	UL=CComplex_num(1,0)/Us;
	IL=CComplex_num(1,0)/Is;
}

//////////////////////////////////////////////////////////
//The Cheby LPF
double CChebyLPF::ChebyData[7][7]={
	{1},
	{1.3472,1.4829},
	{1.1811,1,8214,1.1811},
	{0.9500,1.9582,1.7608,1.0457},
	{0.9766,1.6849,2.0366,1.6849,0.9766},
	{0.8514,1.7956,1.8411,2.0266,1.6312,0.9372},
	{0.9127,1.5947,2.0021,1.8704,2.0021,1.5947,0.9127}};
CChebyLPF::CChebyLPF():CLPF()
{
}
CChebyLPF::~CChebyLPF()
{
}
CChebyLPF::CChebyLPF(int sectionnum):CLPF()
{
	SectionNum=sectionnum;
}
void CChebyLPF::JudgeNeedNum(void)//利用已知条件得到 节数N
{//未完
	SectionNum=5;
}

void CChebyLPF::SetVal(CCCompnent *pCCompnentIn,
						CLCompnent *pLCompnentIn)
{
	for(int i=0;i<SectionNum;i++)
	{
		if(i%2!=0)
		{
			pLCompnent[i/2] =pLCompnentIn[i/2];  // is a L
			pCCompnent[i]= CCCompnent(0);
		}
		else pCCompnent[i] =pCCompnentIn[i];  // is a C 
	}
}

void CChebyLPF::GetVal(CCCompnent *pCCompnentOut,
				  CLCompnent *pLCompnentOut)
{
	for(int i=0;i<SectionNum;i++)
	{
		if(i%2!=0)	
		{
			pLCompnentOut[i/2] =pLCompnent[i/2]; //is a L
			pCCompnentOut[i]= CCCompnent(0);
		}
		else pCCompnentOut[i] =pCCompnent[i]; // is a C
	}
}

void CChebyLPF::CountValue()  //根据节数,查表得出元件
{//C1 L2 C3 L4 C5 L6 C7    R2 R4 R6
//INITTION THE COMPNETN
	delete [] pCCompnent;
	delete [] pLCompnent;
	pCCompnent= new CCCompnent[SectionNum];//[(SectionNum+1)/2];
	pLCompnent= new CLCompnent[SectionNum];//[SectionNum/2];

	double Omg=2*PI*PassFreq;
	double C=1.0/(Omg*RL);
	double L=RL/Omg;
	int i;
	for(i=0;i<SectionNum;i++)
	{
		if(i%2!=0) 
		{
			CLCompnent *pL=new CLCompnent(L*ChebyData[SectionNum-1][i]);
		    pLCompnent[i/2]	=*pL;
			delete pL;
			pCCompnent[i]= CCCompnent(0);
		}
		else
		{
			CCCompnent *pC=new CCCompnent(C*ChebyData[SectionNum-1][i]);
			pCCompnent[i]=*pC;
	        delete pC;
		}
	}
}

void CChebyLPF::GetOut(CComplex_num &UL,CComplex_num &IL,double freq) //不同频率下的复数值大小
{//C1 L2 C3 L4 C5 L6 C7    R2 R4 R6
	int i;
    CComplex_num AllRL(RL,0), Is(1,0), Us(1,0);
	for(i=SectionNum-1;i>=0;i--)
	{
		if(i%2!=0) //is a L Compnent
		{
			CComplex_num L1=pLCompnent[i/2].GetComplexVal(freq);
			Us=(L1+AllRL)/AllRL *Us; //UL is 1
			AllRL=AllRL+L1;
		}
		else  //is a C Compnent
		{
			CComplex_num C1=pCCompnent[i].GetComplexVal(freq);
			Is=(C1+AllRL)/C1  *Is; //IL is 1
			AllRL=AllRL%C1;
		}
	}
	UL=CComplex_num(1,0)/Us;
	IL=CComplex_num(1,0)/Is;
}

////////////////////////////////////////////////////////////////////
//The Cauer LPF

CCauerLPF::CCauerLPF():CLPF()
{
	Reflex=10;
}
CCauerLPF::~CCauerLPF()
{
}
CCauerLPF::CCauerLPF(int sectionnum):CLPF()
{
	Reflex=10;
	SectionNum=sectionnum;
}

CCauerLPF::CCauerLPF(int sectionnum,int reflex):CLPF()
{
	Reflex=reflex;
	SectionNum=sectionnum;
}

double CCauerLPF::CountOs(void) //计算两点比值
{
	return EffectFreq/PassFreq;
}

void CCauerLPF::SetVal(CCCompnent *pCCompnentIn,
						CLCompnent *pLCompnentIn)
{
	for(int i=0;i<SectionNum;i++)
	{
		if(i%2!=0)	pLCompnent[i/2] =pLCompnentIn[i/2];
		pCCompnent[i] =pCCompnentIn[i];
	}
}

void CCauerLPF::GetVal(CCCompnent *pCCompnentOut,
				  CLCompnent *pLCompnentOut)
{
	for(int i=0;i<SectionNum;i++)
	{
		if(i%2!=0)	pLCompnentOut[i/2] =pLCompnent[i/2];
		pCCompnentOut[i] =pCCompnent[i];
	}
}

void CCauerLPF::GetDataFromTable(void)//查表得到归一化值
{
	int i;
	int Seta=int(asin(1.0/CountOs())/PI*180.0)+1;
	char FileName[15]="";
	FILE *fp;
	sprintf( FileName, "Cauer%d.txt",Reflex);
    if( (fp = fopen( FileName, "r" )) == NULL )
      printf( "The file 'data' was not opened\n" );  
/*    CFile f;
    CFileException e;
    char* pFileName = "test.dat";
    if(!f.Open( pFileName, CFile::modeCreate | CFile::modeWrite, &e )) 
	{
      #ifdef _DEBUG  
      afxDump << "File could not be opened " << e.m_cause << "\n";
      #endif  
	}
*/
	char ch;
	int TableSeta=1;
	char Buffer[10];
	int AllNums = SectionNum+SectionNum/2;
	fseek(fp,0L,SEEK_SET);
	while(!feof(fp))
	{
		ch=(char)fgetc(fp);
		if(ch=='\n') TableSeta++;
		if(TableSeta==Seta)
		{
			for(i=0;i<AllNums;i++)
			{
				int p=0;
				ch=(char)fgetc(fp);
				while(ch!=','&&ch!='\n') { Buffer[p++]=ch; ch=(char)fgetc(fp); }
				Buffer[p]='\0';

				if((i+1)%3==0) //is a L 
				{	CLCompnent *pL=&pLCompnent[(i-2)/3];
				    pL->SetVal(atof(Buffer));
				}
				else //is a C
				{
					CCCompnent *pC=&pCCompnent[i-(i+1)/3];
					pC->SetVal(atof(Buffer));	
				}
			}
		    break;
		}
	}
}
void CCauerLPF::GetResonance(double *ResonanceDot,int &DotNum)
{
	DotNum=(SectionNum-1)/2;
	if(DotNum>0)
	{
	 int i;
	 double LC;
//	 ResonanceDot=new double[DotNum];
	 for(i=0;i<DotNum;i++)
	 {
		 LC=pCCompnent[i*2+1].GetVal()*pLCompnent[i].GetVal();
		 ResonanceDot[i]=1.0/(2*PI*sqrt(LC));
	 }
	}
}

void CCauerLPF::JudgeNeedNum(void)//利用已知条件得到 节数N
{//未完
	SectionNum=5;
	Reflex=10;
}
void CCauerLPF::CountValue()  //根据节数,查表得出元件
{//C1 L2 C3 L4 C5 L6 C7    R2 R4 R6
//INITTION THE COMPNETN
	delete [] pCCompnent;
	delete [] pLCompnent;
	pCCompnent= new CCCompnent[SectionNum];
	pLCompnent= new CLCompnent[SectionNum/2];

	GetDataFromTable();//从表中查到数 放入pCCompnent,pLCompnent中

	double Omg=2*PI*PassFreq;
	double C=1.0/(Omg*RL);
	double L=RL/Omg;
	int i;
	for(i=0;i<SectionNum;i++)
	{
		if(i%2!=0)
		{
			CLCompnent *pL=&pLCompnent[i/2];
		    pL->SetVal(pL->GetVal()*L);
		}
		CCCompnent *pC=&pCCompnent[i];
		pC->SetVal(pC->GetVal()*C);
	}
}
void CCauerLPF::GetOut(CComplex_num &UL,CComplex_num &IL,double freq) //不同频率下的复数值大小
{//C1 L2 C3 L4 C5 L6 C7    R2 R4 R6
	int i;
    CComplex_num AllRL(RL,0), Is(1,0), Us(1,0);

	for(i=SectionNum-1;i>=0;i--)
	{
		if(i%2!=0) //is a L Compnent
		{
			CComplex_num LC1=pLCompnent[i/2].GetComplexVal(freq)%
			             	 pCCompnent[i].GetComplexVal(freq);
			Us= (LC1+AllRL)/AllRL *Us; //UL is 1
			AllRL=AllRL+LC1;
		}
		else //is a C Compnent
		{
			CComplex_num C1=pCCompnent[i].GetComplexVal(freq);
			Is= (C1+AllRL)/C1 *Is; 
			AllRL=AllRL%C1;
		}
	}
	UL=CComplex_num(1,0)/Us;
	IL=CComplex_num(1,0)/Is;
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -