Re: демодулятор SSB
Добавлено: 08 июн 2018, 12:15
DEL
Теория и практика цифровой обработки сигналов
http://ru.dsplib.org/forum/
Код: Выделить всё
#include <DueTimer.h>
#define Ntap 31
#define DCgain 32768
int myLed = 13;
int OutSample,I,Q,N,X1,X2,X3,Y1,Y2,Y3;
float count=0;
int led_flag=0;
int cos_N[4]={1,0,-1,0};
int sin_N[4]={0,1,0,-1};
int sin_N_m[4]={0,-1,0,1};
bool ledOn = false;
// Фильтр канала Q
int fir_Q(int NewSample) {
int FIRCoef[Ntap] = {
-98,
-92,
159,
143,
-265,
-220,
450,
335,
-792,
-490,
1469,
671,
-3046,
-827,
10313,
17347,
10313,
-827,
-3046,
671,
1469,
-490,
-792,
335,
450,
-220,
-265,
143,
159,
-92,
-98
};
static int x[Ntap]; //input samples
long int y=0; //output sample
int n;
//shift the old samples
for(n=Ntap-1; n>0; n--)
x[n] = x[n-1];
//Calculate the new output
x[0] = NewSample;
for(n=0; n<Ntap; n++)
y += FIRCoef[n] * x[n];
return y / DCgain;
}
// Фильтр канала I
int fir_I(int NewSample) {
int FIRCoef[Ntap] = {
-98,
-92,
159,
143,
-265,
-220,
450,
335,
-792,
-490,
1469,
671,
-3046,
-827,
10313,
17347,
10313,
-827,
-3046,
671,
1469,
-490,
-792,
335,
450,
-220,
-265,
143,
159,
-92,
-98
};
static int x[Ntap]; //input samples
long int y=0; //output sample
int n;
//shift the old samples
for(n=Ntap-1; n>0; n--)
x[n] = x[n-1];
//Calculate the new output
x[0] = NewSample;
for(n=0; n<Ntap; n++)
y += FIRCoef[n] * x[n];
return y / DCgain;
}
void myHandler(){ // обработчик прерывания по таймеру
if (N==3)
{
N=0;
}
else N++;
I=analogRead(A0);
Q=analogRead(A1);
X1=I*cos_N[N]+Q*sin_N[N];
X2=fir_I(X1);
X3=X2*cos_N[N];
Y1=I*sin_N_m[N]+Q*cos_N[N];
Y2=fir_Q(Y1);
Y3=Y2*sin_N_m[N];
OutSample=X3+Y3;
OutSample=OutSample;// восстанавливаем однополярный сигнал
analogWrite(DAC0, OutSample);
}
void setup(){
pinMode(myLed, OUTPUT);
Timer3.attachInterrupt(myHandler);
Timer3.start(500); // Calls every 50ms
pinMode(DAC0, OUTPUT);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
REG_PIOC_OER = 0x1 <<22;
REG_PIOB_OER = 0x1 <<27;
analogWriteResolution(12);
analogReadResolution(12);
}
void loop(){
while(1){
REG_PIOC_SODR = 0x1 << 22; //c22 = 8 pin
REG_PIOB_CODR = 0x1 << 27; //b27=13 pin
delay(1000);
REG_PIOC_CODR = 0x1 << 22; //c22=8 pin
REG_PIOB_SODR = 0x1 << 27; //b27 = 13 pin
delay(1000);
}
}
Код: Выделить всё
#include <DueTimer.h>
#define Ntap 31
#define DCgain 32768
int myLed = 13;
int OutSample,I,Q,N,X1,X2,X3,Y1,Y2,Y3;
float count=0;
int led_flag=0;
int cos_N[4]={1,0,-1,0};
int sin_N[4]={0,1,0,-1};
int sin_N_m[4]={0,-1,0,1};
bool ledOn = false;
// Фильтр канала Q
int fir_Q(int NewSample) {
int FIRCoef[Ntap] = {
-92,
159,
143,
-265,
-220,
450,
335,
-792,
-490,
1469,
671,
-3046,
-827,
10313,
17347,
10313,
-827,
-3046,
671,
1469,
-490,
-792,
335,
450,
-220,
-265,
143,
159,
-92,
-98
};
static int x[Ntap]; //input samples
long int y=0; //output sample
int n;
//shift the old samples
for(n=Ntap-1; n>0; n--)
x[n] = x[n-1];
//Calculate the new output
x[0] = NewSample;
for(n=0; n<Ntap; n++)
y += FIRCoef[n] * x[n];
return y / DCgain;
}
// Фильтр канала I
int fir_I(int NewSample) {
int FIRCoef[Ntap] = {
-98,
-92,
159,
143,
-265,
-220,
450,
335,
-792,
-490,
1469,
671,
-3046,
-827,
10313,
17347,
10313,
-827,
-3046,
671,
1469,
-490,
-792,
335,
450,
-220,
-265,
143,
159,
-92,
-98
};
static int x[Ntap]; //input samples
long int y=0; //output sample
int n;
//shift the old samples
for(n=Ntap-1; n>0; n--)
x[n] = x[n-1];
//Calculate the new output
x[0] = NewSample;
for(n=0; n<Ntap; n++)
y += FIRCoef[n] * x[n];
return y / DCgain;
}
void myHandler(){ // обработчик прерывания по таймеру
if (N==3)
{
N=0;
}
else N++;
I=analogRead(A0);
Q=analogRead(A1);
X1=I*cos_N[N]+Q*sin_N[N];
X2=fir_I(X1);
X3=X2*cos_N[N];
Y1=I*sin_N_m[N]+Q*cos_N[N];
Y2=fir_Q(Y1);
Y3=Y2*sin_N_m[N];
OutSample=X3+Y3;
OutSample=OutSample;// восстанавливаем однополярный сигнал
analogWrite(DAC1, OutSample);
}
void setup(){
pinMode(myLed, OUTPUT);
Timer3.attachInterrupt(myHandler);
Timer3.start(500); // Calls every 50ms
pinMode(DAC1, OUTPUT);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
REG_PIOC_OER = 0x1 <<22;
REG_PIOB_OER = 0x1 <<27;
// analogWriteResolution(12);
// analogReadResolution(12);
}
void loop(){
while(1){
REG_PIOC_SODR = 0x1 << 22; //c22 = 8 pin
REG_PIOB_CODR = 0x1 << 27; //b27=13 pin
delay(1000);
REG_PIOC_CODR = 0x1 << 22; //c22=8 pin
REG_PIOB_SODR = 0x1 << 27; //b27 = 13 pin
delay(1000);
}
}