Découvrez notre Chaîne YouTube "Ingénierie et Projets"
Découvrez notre Chaîne Secondaire "Information Neuronale et l'Ingénierie du Cerveau"
La page contient la synthèse (pas toutes) des fonctions utilisées dans les projets. La page sera mis à jour de temps en temps. Les fonctions récentes seront intégrées à la fin de la page. Vous pouvez consulter la page si vous avez des difficultés à trouver une fonction dans le code fournit du projet ou effectuer une recherche par mot clés dans le blog.
Onduleur triphasé
void SetCMD1( int *pins)
{
static int I=0;
const bool Cmd[6][6]={{0,1,1,0,0,1},
{1,0,1,0,0,1},
{1,0,0,1,0,1},
{1,0,0,1,1,0},
{0,1,0,1,1,0},
{0,1,1,0,1,0}};
for (int i=0;i<6; i++) digitalWrite(pins[i], Cmd[I][i]);
I++; I%=6;
return 0;
}
void SetCMD2( int *pins)
{
static unsigned char I=0;
const unsigned char Cmd[6]={B00011001,
B00101001,
B00100101,
B00100110,
B00010110,
B00011010};
PORTA=Cmd[I];
I++;
//I%=6; // Fréquence: +
//I=I*(I<6); // Fréquence: ++
if (I>=6)I=0; // Fréquence: +++
return 0;
}
void SetCMD3( int *pins)
{
static unsigned char I=0;
const bool Cmd[12][6]={
{0,1,1,0,0,0},
{0,0,1,0,0,1},
{0,0,1,0,0,1},
{1,0,0,0,0,1},
{1,0,0,0,0,1},
{1,0,0,1,0,0},
{1,0,0,1,0,0},
{0,0,0,1,1,0},
{0,0,0,1,1,0},
{0,1,0,0,1,0},
{0,1,0,0,1,0},
{0,1,1,0,0,0},
};
for (int i=0;i<6; i++) digitalWrite(pins[i], Cmd[I][i]);
I++;
if (I>=12)I=0;
return 0;
}
void SetCMD4( int *pins)
{
static unsigned char I=0;
const unsigned char Cmd[12]= {
B00011000,
B00001001,
B00001001,
B00100001,
B00100001,
B00100100,
B00100100,
B00000110,
B00000110,
B00010010,
B00010010,
B00011000
};
PORTA=Cmd[I];
I++;
if (I>=12)I=0;
return 0;
}
float DFT(float *data, int Ndata, float *real, float *imag, float fs)
{
int nfft=floor((float)Ndata/2.0);
float df=(fs/2.0)/(float)nfft;
float somme_r=0.0, somme_i=0.0;
float wt=0.0;
for (int i=0; i<nfft; i++)
{
for (int j=0; j<Ndata; j++)
{
wt=-2.0*PI*(float)i*(float)j/Ndata;
somme_r+=data[j]*cos(wt);
somme_i+=data[j]*sin(wt);
}
real[i]= somme_r; somme_r=0.0;
imag[i]= somme_i; somme_i=0.0;
}
return df;
}
float DFTn(float *data, int Ndata, float *realImag, int n, float fs)
{
int nfft=floor((float)Ndata/2.0);
float df=(fs/2.0)/(float)nfft;
float somme_r=0.0, somme_i=0.0;
float wt=0.0;
if (n>=nfft) return 0.0;
int i=n;
for (int j=0; j<Ndata; j++)
{
wt=-2.0*PI*(float)i*(float)j/(float)Ndata;
somme_r+=data[j]*cos(wt);
somme_i+=data[j]*sin(wt);
}
realImag[0]= somme_r;
realImag[1]= somme_i;
return df;
}
float getFreqHz(int Ai, int s_max, int s_min)
{
static long T_2=0, T_1=0;
static int ai_1=0, ai_2=0;
static float freq=0.0;
ai_1=analogRead(54+Ai);
if((ai_2-ai_1)>s_max)
{
T_2=micros()-T_1;
freq=1.0/((float)T_2*1E-6);
T_1=micros();
}
ai_2=ai_1;
return freq;
}
float setWindow(float *datain, float *dataout, int n, int type)
{
float Hi, t=0.0,t0=0.0, sig=0.0;
const float gain[6]={0.5581,0.5934,0.4765,0.6456,0.5581,1.0000};
switch(type)
{
//Fenêtre de Hann
case 0:
for(int i=0;i<n; i++)
{
t=(float)i/float(n);
Hi=0.5-0.5*cos(2.0*PI*t);
dataout[i]=Hi*datain[i];
}
return gain[type];
//Fenêtre de Hamming
case 1:
for(int i=0;i<n; i++)
{
t=(float)i/float(n);
Hi=0.54-0.46*cos(2.0*PI*t);
dataout[i]=Hi*datain[i];
}
return gain[type];
// Fenêtre de Blackman
case 2:
for(int i=0;i<n; i++)
{
t=(float)i/float(n);
Hi=0.42-0.5*cos(2.0*PI*t)+0.08*cos(4.0*PI*t);
dataout[i]=Hi*datain[i];
}
return gain[type];
//Fenêtre de Gauss
case 3:
for(int i=0;i<n; i++)
{
t=((float)i/float(n))-0.5;
t0=0.5;
sig=t0/2.0;
Hi=exp(-0.5*t*t/(sig*sig));
dataout[i]=Hi*datain[i];
}
return gain[type];
//Fenêtre sin²
case 4:
for(int i=0;i<n; i++)
{
t=(float)i/float(n);
Hi=sin(2.0*0.5*PI*t)*sin(2.0*0.5*PI*t); // sin(x)²=(1-cos(2x))/2
dataout[i]=Hi*datain[i];
}
return gain[type];
default:
for(int i=0;i<n; i++)
{
dataout[i]=datain[i];
}
return gain[5];
}
}
Diverses
void getMax(float *datain, int n, float *maxid)
{
float maxe=datain[0];
float id=0;
for (int i=1;i<n; i++)
{
if (datain[i]>=maxe)
{
maxe=datain[i];
id=i;
}
}
maxid[0]=maxe;
maxid[1]=id;
}
double rand10(void)
{
double rn1=2.0*((random(1e10)/1e10)-0.5);
double rn2=2.0*((random(1e10)/1e10)-0.5);
double rn=tanh((rn1-rn2));
return rn;
}
float getData(float *dataout,int nn, unsigned long ts_us, int analogpin, float gain, int noDC)
{
// Lecture des données brutes
float ts_f=0.0;
unsigned long t11;
for(int i=0; i<nn; i++)
{
t11=micros();
dataout[i]=(float)analogRead(54+analogpin);
delayMicroseconds(ts_us);
ts_f+=(float)micros()-(float)t11;
}
ts_f=ts_f/(float)nn;
// Conversion en V
for(int i=0; i<nn; i++)
dataout[i]=gain*dataout[i]*3.3/4095.0;
// Suppression de la valeur DC
if(noDC==0)
{
float Vm=0.0;
for(int i=0; i<nn;i++) Vm+=dataout[i];
Vm/=(float)nn;
for(int i=0; i<nn;i++) dataout[i]=dataout[i]-Vm;
}
return 1E6/ts_f;
}