//============================================================================
// Name : filter.cpp
// Author : Olavi Suurmägi
olavsu1@gmail.com
// Version : 0.3a
// Copyright : (c) 2009 Olavi Suurmägi
olavsu1@gmail.com
// Description : Ühe, kahe ja kolmeribaliste kõlarite sagedusfilti arvutus.
//============================================================================
#include <iostream>
#include <cmath>
#include <fstream>
#include <sstream>
using namespace std;
int main() {
short Spk, K;
double Lv, Lvm, Lvkes, Ck, Ckm, Ckkes, Z, Zm, Zkes, Fdw, a1, a2, a3, Rk, Rm,
Rkes, L1, L2, L3, L4, L5, L6, C1, C2, C3 ,C4 ,C5, C6,
//põhiühikute teisenduse jaoks.
Cp = 1000000, // F -> µF
Lp = 1000; // H -> mH
stringstream sstr;
/*sagedused*/
short Fd1 = 500, //madaldsageduse ülempiir
Fd2 = 5000, //kõrgsageduse alampiir
DeltaFd = Fd2-Fd1; //kesksagedus ala
cout << "Mitme ribaline on sinu kõlar? Woofri puhul vali '1' (1, 2, 3)" << endl;
cin >> Spk;
if((Spk < 1) || (Spk > 3))
cerr << "Mõtle ikka järgi, mis sa teha tahtsid." << endl;
else if(Spk == 2)
{
cout << "Kaheribakõlar valitud" << endl;
cout << "Anna kõrgsagedus valjuhääldi takistus oomides." << endl;
cin >> Rk;
cout << "Anna madalsagedus valjuhääldi takistus oomides." << endl;
cin >> Rm;
cout << "Anna madalsagedus valjuhääldi |Z| takistus oomides - leiad selle vastava valjuhääldi passis olevast graafikust väärtuse 10000 Hz juurest"<< endl;
cin >> Zm;
cout << "Mitmendat järku on sinu filter? (1, 2, 3)" << endl;
cin >> K;
if(K == 1)
{
/*konstandid*/
a1 = 1;
//valemid põhiühikutes
L1 = a1*Rm/(2*M_PI*Fd2); //L1(ms)
C1 = 1/(2*M_PI*Fd2*Rk*a1);//C1(ks)
//väljund koos teisendamisega
sstr << "Kondensaatorid - Capasitors" <<endl;
sstr << endl;
sstr << " C1 = " << C1*Cp <<" µF" << endl;
sstr << endl;
sstr << "Induktiivpoolid - Coils" << endl;
sstr << endl;
sstr << " L1 = " << L1*Lp <<" mH" << endl;
sstr << endl;
sstr <<"Selles filtris on L1 jadaühenduses madalsagedus valjuhääldiga ja"<< endl;
sstr <<"C1 jadaühenduses kõrgsagedus valjuhäädiga."<< endl;
//kompensatsioon
Lvm = Zm/(2*M_PI*10000);
Ckm = Lvm/(Rm*Rm);
sstr << "madalsageduse kompensatsioon - RC jadaahel rööbiti madalsagedus valjuhääldiga" << endl;
sstr << endl;
sstr << "R =" << Rm <<" Ω"<< endl;
sstr << "C" << Ckm*Cp <<" µF, põhiühikutes " << Ckm <<" F"<< endl;
cout << sstr.str();
ofstream myfile ("2ribakõlar_1järk.txt");
if (myfile.is_open())
{
myfile << sstr.str();
myfile.close();
cout << "Tulemus kirjutati faili 2ribakõlar_1järk.txt" << endl;
}
else
cerr << "Ei saa faili kirjutada" << endl;
}//esimest järku filter
else if(K == 2)
{
/*konstandid*/
a1 = 2;
a2 = 1.5000;
//valemid põhiühikutes
L1 = a1*Rm/(2*M_PI*Fd2); //L1(ms)
C1 = a2/(2*M_PI*Fd2*Rm); //C2(ms)
C2 = 1/(2*M_PI*Fd2*Rk*a1); // C1(ks)
L2 = Rk/(2*M_PI*Fd2*a2); //L2(ks)
//väljund koos teisendamisega
sstr << "Kondensaatorid - Capasitors" <<endl;
sstr << endl;
sstr << " C1 = " << C1*Cp <<" µF" << endl;
sstr << " C2 = " << C2*Cp <<" µF" << endl;
sstr << endl;
sstr << "Induktiivpoolid - Coils" << endl;
sstr << endl;
sstr << " L1 = " << L1*Lp <<" mH" << endl;
sstr << " L2 = " << L2*Lp <<" mH" << endl;
//kompensatsioon
Lvm = Zm/(2*M_PI*10000);
Ckm = Lvm/(Rm*Rm);
sstr << "madalsageduse kompensatsioon - RC jadaahel rööbiti madalsagedus valjuhääldiga" << endl;
sstr << endl;
sstr << "R = " << Rm <<" Ω"<< endl;
sstr << "C = " << Ckm*Cp <<" µF, põhiühikutes " << Ckm <<" F"<< endl;
cout << sstr.str();
ofstream myfile ("2ribakõlar_2järk.txt");
if (myfile.is_open())
{
myfile << sstr.str();
myfile.close();
cout << "Tulemus kirjutati faili 2ribakõlar_2järk.txt" << endl;
}
else
cerr << "Ei saa faili kirjutada"<< endl;
}//teist järku filter
else if(K == 3)
{
/*konstandid*/
a1 = 1.5000;
a2 = 1.3333;
a3 = 0.5000;
//valemid põhiühikutes
L1 = a1*Rm/(2*M_PI*Fd2); //L1(ms)
C1 = a2/(2*M_PI*Fd2*Rm); //C2(ms)
L2 = a3*Rm/(2*M_PI*Fd2); //L3(ms)
C2 = 1/(2*M_PI*Fd2*Rk*a1); // C1(ks)
L3 = Rk/(2*M_PI*Fd2*a2); //L2(ks)
C3 = 1/(2*M_PI*Fd2*Rk*a3); // C1(ks)
//väljund koos teisendamisega
sstr << "Kondensaatorid - Capasitors" <<endl;
sstr << endl;
sstr << " C1 = " << C1*Cp <<" µF" << endl;
sstr << " C2 = " << C2*Cp <<" µF" << endl;
sstr << " C3 = " << C3*Cp <<" µF" << endl;
sstr << endl;
sstr << "Induktiivpoolid - Coils" << endl;
sstr << endl;
sstr << " L1 = " << L1*Lp <<" mH" << endl;
sstr << " L2 = " << L2*Lp <<" mH" << endl;
sstr << " L3 = " << L3*Lp <<" mH" << endl;
//kompensatsioon
Lvm = Zm/(2*M_PI*10000);
Ckm = Lvm/(Rm*Rm);
sstr << "madalsageduse kompensatsioon - RC jadaahel rööbiti madalsagedus valjuhääldiga" << endl;
sstr << endl;
sstr << "R = " << Rm <<" Ω"<< endl;
sstr << "C = " << Ckm*Cp <<" µF, põhiühikutes " << Ckm <<" F"<< endl;
cout << sstr.str();
ofstream myfile ("2ribakõlar_3järk.txt");
if (myfile.is_open())
{
myfile << sstr.str();
myfile.close();
cout << "Tulemus kirjutati faili 2ribakõlar_3järk.txt";
}
else
cerr <<"Ei saa falili kirjutada" << endl;
return 0;
}//kolmandat järku filter
}//kaheribakõlar
else if(Spk == 3)
{
cout << "kolmeribakõlar valitud" << endl;
cout << "anna kõrgsagedus valjuhääldi takistus oomides" << endl;
cin >> Rk;
cout << "anna kesksagedus valjuhääldi takistus oomides" << endl;
cin >> Rkes;
cout << "anna kesksagedus valjuhääldi |Z| takistus oomides - leiad selle vastava valjuhääldi passis olevast graafikust väärtuse 10000 Hz juurest"<< endl;
cin >> Zkes;
cout << "anna madalsagedus valjuhääldi takistus oomides" << endl;
cin >> Rm;
cout << "anna madalsagedus valjuhääldi |Z| takistus oomides - leiad selle vastava valjuhääldi passis olevast graafikust väärtuse 10000 Hz juurest"<< endl;
cin >> Zm;
cout << "mitmendat järku on sinu filter? (1, 2, 3)" << endl;
cin >> K;
if(K == 1)
{
/*konstandid*/
a1 = 1;
//valemid põhiühikutes
L1 = a1*Rm/(2*M_PI*Fd1); //L1(ms)
L2 = a1*Rkes/(2*M_PI*DeltaFd); //L1(kes)
C1 = 1/(4*(M_PI*M_PI)*Fd2*Fd1*L2); //C1(kes)
C2 = 1/(2*M_PI*Fd2*Rk*a1); // C1(ks)
//väljund koos teisendamisega
sstr << "Kondensaatorid - Capasitors" <<endl;
sstr << endl;
sstr << " C1 = " << C1*Cp <<" µF" << endl;
sstr << " C2 = " << C2*Cp <<" µF" << endl;
sstr << endl;
sstr << "Induktiivpoolid - Coils" << endl;
sstr << endl;
sstr << " L1 = " << L1*Lp <<" mH" << endl;
sstr << " L2 = " << L2*Lp <<" mH" << endl;
//korreksioon, vajalik on konkreetse valjuhääldi |Z|
Lvm = Zm/(2*M_PI*10000);
Ckm = Lvm/(Rm*Rm);
Lvkes = Zkes/(2*M_PI*10000);
Ckkes = Lvkes/(Rkes*Rkes);
sstr << "madalsageduse kompensatsioon - RC jadaahel rööbiti madalsagedus valjuhääldiga" << endl;
sstr << endl;
sstr << "R = " << Rm <<" Ω"<< endl;
sstr << "C = " << Ckm*Cp <<" µf, põhiühikutes " << Ckm <<" F"<< endl;
sstr << "kesksageduse kompensatsioon - RC jadaahel rööbiti kesksagedus valjuhääldiga" << endl;
sstr << endl;
sstr << "R = " << Rkes <<" Ω"<< endl;
sstr << "C = " << Ckkes*Cp <<" µF, põhiühikutes " << Ckkes <<" F"<< endl;
cout << sstr.str();
ofstream myfile ("3ribakõlar_1järk.txt");
if (myfile.is_open())
{
myfile << sstr.str();
myfile.close();
cout << "Tulemus kirjutati faili 3ribakõlar_1järk.txt" << endl;
}
else
cerr <<"Ei saa falili kirjutada" << endl;
}//esimest järku filter
else if(K == 2)
{
/*konstandid*/
a1 = 2;
a2 = 1.5000;
//valemid põhiühikutes
L1 = a1*Rm/(2*M_PI*Fd1); //L1(ms) põhiühikutes
C1 = a2/(2*M_PI*Fd1*Rm); //C2(ms)
L2 = a1*Rkes/(2*M_PI*DeltaFd); //L1(kes)
C2 = 1/(4*(M_PI*M_PI)*Fd1*Fd2*L2); //c1(kes)
C3 = a2/(2*M_PI*DeltaFd*Rkes); //c2(kes)
L3 = 1/(4*(M_PI*M_PI)*Fd1*Fd2*C3); //L2(kes)
C4 = 1/(2*M_PI*Fd2*Rk*a1); //C1(ks)
L4 = Rk/(2*M_PI*Fd2*a2); //l2(ks)
//väljund koos teisendamisega
sstr << "Kondensaatorid - Capasitors" <<endl;
sstr << endl;
sstr << " C1 = " << C1*Cp <<" µF" << endl;
sstr << " C2 = " << C2*Cp <<" µF" << endl;
sstr << " C3 = " << C3*Cp <<" µF" << endl;
sstr << " C4 = " << C4*Cp <<" µF" << endl;
sstr << endl;
sstr << "Induktiivpoolid - Coils" << endl;
sstr << endl;
sstr << " L1 = " << L1*Lp <<" mH" << endl;
sstr << " L2 = " << L2*Lp <<" mH" << endl;
sstr << " L3 = " << L3*Lp <<" mH" << endl;
sstr << " L4 = " << L4*Lp <<" mH" << endl;
//korreksioon, vajalik on konkreetse valjuhääldi |Z|
Lvm = Zm/(2*M_PI*10000);
Ckm = Lvm/(Rm*Rm);
Lvkes = Zkes/(2*M_PI*10000);
Ckkes = Lvkes/(Rkes*Rkes);
sstr << "madalsageduse kompensatsioon - RC jadaahel rööbiti madalsagedus valjuhääldiga" << endl;
sstr << endl;
sstr << "R = " << Rm <<" Ω"<< endl;
sstr << "C = " << Ckm*Cp <<" µF, põhiühikutes " << Ckm <<" F"<< endl;
sstr << "kesksageduse kompensatsioon - RC jadaahel rööbiti kesksagedus valjuhääldiga" << endl;
sstr << endl;
sstr << "R = " << Rkes <<" Ω"<< endl;
sstr << "C = " << Ckkes*Cp <<" µF, põhiühikutes " << Ckkes <<" F"<< endl;
cout << sstr.str();
ofstream myfile ("3ribakõlar_2järk.txt");
if (myfile.is_open())
{
myfile << sstr.str();
myfile.close();
cout << "Tulemus kirjutati faili 3ribakõlar_2järk.txt" << endl;
}
else
cerr << "Ei saa falili kirjutada" << endl;
}//teist järku filter
else if(K == 3)
{
/*konstandid*/
a1 = 1.5000;
a2 = 1.3333;
a3 = 0.5000;
//valemid põhiühikutes
L1 = a1*Rm/(2*M_PI*Fd1);//L1(ms)
C1 = a2/(2*M_PI*Fd1*Rm);//C2(ms)
L2 = a3*Rm/(2*M_PI*Fd1);//L3(ms)
L3 = a1*Rkes/(2*M_PI*DeltaFd); //L1(kes)
C2 = 1/(4*(M_PI*M_PI)*Fd1*Fd2*L3);//C1(kes)
C3 = a2/(2*M_PI*DeltaFd*Rkes); //C2(kes)
L4 = 1/(4*(M_PI*M_PI)*Fd1*Fd2*C3); //L2(kes)
L5 = a3*Rkes/(2*M_PI*DeltaFd); //L3(kes)
C4 = 1/(4*(M_PI*M_PI)*Fd1*Fd2*L5);//C3(kes)
C5 = 1/(2*M_PI*Fd2*a1);//C1(ks)
L6 = Rk/(2*M_PI*Fd2*a2);//L2(ks)
C6 =1/(2*M_PI*Fd2*Rk*a3); //C3(ks)
//väljund koos teisendamisega
sstr << "Kondensaatorid - Capasitors" <<endl;
sstr << endl;
sstr << " C1 = " << C1*Cp <<" µF" << endl;
sstr << " C2 = " << C2*Cp <<" µF" << endl;
sstr << " C3 = " << C3*Cp <<" µF" << endl;
sstr << " C4 = " << C4*Cp <<" µF" << endl;
sstr << " C5 = " << C5*Cp <<" µF" << endl;
sstr << " C6 = " << C6*Cp <<" µF" << endl;
sstr << endl;
sstr << "Induktiivpoolid - Coils" << endl;
sstr << endl;
sstr << " L1 = " << L1*Lp <<" mH" << endl;
sstr << " L2 = " << L2*Lp <<" mH" << endl;
sstr << " L3 = " << L3*Lp <<" mH" << endl;
sstr << " L4 = " << L4*Lp <<" mH" << endl;
sstr << " L5 = " << L5*Lp <<" mH" << endl;
sstr << " L6 = " << L6*Lp <<" mH" << endl;
//korreksioon, vajalik on konkreetse valjuhääldi |Z|
Lvm = Zm/(2*M_PI*10000);
Ckm = Lvm/(Rm*Rm);
Lvkes = Zkes/(2*M_PI*10000);
Ckkes = Lvkes/(Rkes*Rkes);
sstr << "madalsageduse kompensatsioon - RC jadaahel rööbiti madalsagedus valjuhääldiga" << endl;
sstr << endl;
sstr << "R = " << Rm <<" Ω"<< endl;
sstr << "C = " << Ckm*Cp <<" µf, põhiühikutes " << Ckm << " F"<< endl;
sstr << "kesksageduse kompensatsioon - RC jadaahel rööbiti kesksagedus valjuhääldiga" << endl;
sstr << endl;
sstr << "R = " << Rkes <<" Ω"<< endl;
sstr << "C = " << Ckkes*Cp <<" µF, põhiühikutes " << Ckkes << " F"<< endl;
cout << sstr.str();
ofstream myfile ("3ribakõlar_3järk.txt");
if (myfile.is_open())
{
myfile << sstr.str();
myfile.close();
cout << "Tulemus kirjutati faili 3ribakõlar_3järk.txt" << endl;
}
else
cerr <<"Ei saa falili kirjutada" << endl;
}//kolmandat järku filter
}//kolmeribakõlar
else if(Spk == 1)
{
cout << "Woofer valitud" << endl;
cout << "Anna soovitud lõikesagedus, hertsides" << endl;
cin >> Fdw; // tavakõlaril on bassi lõikesagedus 500Hz, woofril on see madalam.
cout << "Anna valjuhääldi takistus" << endl;
cin >> Rm;
cout << "Anna valjuhääldi |Z|takistus - leiad selle vastava valjuhääldi passis olevast graafikust väärtuse 10000 Hz juurest" << endl;
cin >> Z;
/*konstandid*/
a1 = 1.5000;
a2 = 1.3333;
a3 = 0.5000;
//valemid põhiühikutes ainult kolmas järk.
L1 = a1*Rm/(2*M_PI*Fdw);//L1(ms)
C1 = a2/(2*M_PI*Fdw*Rm);//C2(ms)
L2 = a3*Rm/(2*M_PI*Fdw);//L3(ms)
//väljund koos teisendamisega
//korreksioon, vajalik on konkreetse valjuhääldi |Z|
Lv = Z/(2*M_PI*10000); //valjuhääldi induktiivsus sagedusel 10000
Ck = Lv/(Rm*Rm);
cout << endl;
//cout <<"Lv =" << Lv*pow(10, 3) <<" mH"<< endl;
cout << endl;
sstr << "Kondensaatorid - Capasitors" <<endl;
sstr << endl;
sstr << " C1 = " << C1*Cp <<" µF" << endl;
sstr << endl;
sstr << "Induktiivpoolid - Coils" << endl;
sstr << endl;
sstr << " L1 = " << L1*Lp <<" mH" << endl;
sstr << " L2 = " << L2*Lp <<" mH" << endl;
sstr << endl;
sstr << "Kompensatsioon - RC jadaahel rööbiti valjuhääldiga" << endl;
sstr << endl;
sstr << "R = " << Rm <<" Ω"<< endl;
sstr << "C = " << Ck*Cp <<" µF, põhiühikutes " << Ck <<" F"<< endl;
cout << sstr.str();
ofstream myfile ("woofer.txt");
if (myfile.is_open())
{
myfile << sstr.str();
myfile.close();
cout << "Tulemused salvestati faili woofer.txt" << endl;
}
else
cout << "Ei saa faili kirjutada"<< endl;
}
return 0;
}