362 lines
12 KiB
C
362 lines
12 KiB
C
#include <math.h>
|
||
#include <stdint.h>
|
||
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#include <complex.h>
|
||
#include <string.h>
|
||
|
||
#define A 1
|
||
|
||
struct qam_system_s {
|
||
int M; // Nombre de symboles M-QAM
|
||
int k; // Nombre de bits/symboles
|
||
double Fs; // Fréquence d'échantillionage
|
||
double Ts; // Temps d'échantillionage
|
||
int N; // Nombre d'échantillions
|
||
double Fc; // Fréquence de la porteuse
|
||
double complex** constellation; // Tableau de symboles I + j Q
|
||
};
|
||
typedef struct qam_system_s qam_system;
|
||
|
||
// Initialisation de la constellation (double tableau de taille sqrt(M)),
|
||
// ToDo : changer à un tableau à 1 dimension pour éviter de calculer sqrt(M)
|
||
void init_constellation (qam_system* qam) {
|
||
int sm = (int)sqrt(qam->M);
|
||
qam->constellation = (double complex**)malloc(sizeof(double complex*) * sm);
|
||
|
||
for (int i = 0; i < sm; i++) {
|
||
qam->constellation[i] = (double complex*)malloc(sizeof(double complex) * sm);
|
||
}
|
||
|
||
//double norm_factor = sqrt((double)(qam->M - 1) / 3.0); // Pour puissance unitaire
|
||
double norm_factor = sqrt((double)(1.7*(qam->M - 1)/3.0));
|
||
|
||
for (int i = 0; i < sm; i++) {
|
||
double complex ip = -(sm - 1) + 2 * i;
|
||
for (int j = 0; j < sm; j++) {
|
||
double complex qp = -(sm - 1) + 2 * j;
|
||
qam->constellation[i][j] = (ip + I * qp) / norm_factor;
|
||
}
|
||
}
|
||
}
|
||
|
||
// Calcul du bruit gaussien pour un sigma donné
|
||
// Formule de Box-Muller
|
||
double gaussian_noise (double sigma) {
|
||
double u1 = (rand() + 1) / ((double)RAND_MAX + 2);
|
||
double u2 = (rand() + 1) / ((double)RAND_MAX + 2);
|
||
return sigma * sqrt(-2 * log(u1)) * cos(2 * M_PI * u2);
|
||
}
|
||
|
||
// Ajout du bruit
|
||
void add_noise (double complex* s, int len, double sigma) {
|
||
for (int i = 0; i < len; i++) {
|
||
double nr = gaussian_noise(sigma);
|
||
double ni = gaussian_noise(sigma);
|
||
s[i] += nr + I * ni;
|
||
}
|
||
}
|
||
|
||
// Changer le tableau de bits en boolen ou alors la represenation binaire et shifter pour extraire les bits (pas bien si M plus grand)
|
||
void bits_to_symbols (qam_system* qam, uint8_t* bits, int nb_bits, double complex* symbols) {
|
||
int nb_symbols = nb_bits / qam->k;
|
||
int sm = sqrt(qam->M);
|
||
for (int k = 0; k < nb_symbols; k++) {
|
||
int id = 0;
|
||
for (int b = 0 ; b < qam->k; b++) {
|
||
id = id * 2 + bits[k * qam->k + b];
|
||
}
|
||
int i = id / sm;
|
||
int j = id % sm;
|
||
symbols[k] = qam->constellation[i][j];
|
||
}
|
||
}
|
||
|
||
// Modulation QAM
|
||
void modulate (qam_system* qam, double complex* symbols, int nb_symbols, double complex* s) {
|
||
for (int k = 0; k < nb_symbols; k++) {
|
||
double complex iq = symbols[k];
|
||
for (int n = 0; n < qam->N; n++) {
|
||
//s[k * qam->N + n] = A * iq * cexp(2 * I * M_PI * qam->Fc * ((double)n / qam->Fs));
|
||
int idx = k * qam->N + n;
|
||
s[idx] = A * iq * cexp(2 * I * M_PI * qam->Fc * ((double)idx / qam->Fs));
|
||
}
|
||
}
|
||
}
|
||
|
||
// Demodulation QAM
|
||
void demodulate(qam_system* qam, double complex* s, int nb_symbols, uint8_t* bits_hat, FILE *fp_constel) {
|
||
for (int k = 0; k < nb_symbols; k++) {
|
||
double complex r = 0;
|
||
for (int n = 0; n < qam->N; n++) {
|
||
//r += s[k * qam->N + n] * cexp(-2 * I * M_PI * qam->Fc * ((double)n / qam->Fs));
|
||
r += s[k * qam->N + n] * cexp(-2 * I * M_PI * qam->Fc * ((double)(k * qam->N + n) / qam->Fs));
|
||
|
||
|
||
}
|
||
r /= qam->N;
|
||
r /= A;
|
||
|
||
if (fp_constel) {
|
||
fprintf(fp_constel, "% .8f % .8f\n", creal(r), cimag(r));
|
||
fflush(fp_constel);
|
||
}
|
||
|
||
// Distance euclidien de Ir et Qr pour avoir le point le plus proche de la constellation (lent)
|
||
int sm = (int)sqrt(qam->M);
|
||
double min_d = INFINITY;
|
||
int i_cl, j_cl = 0;
|
||
for (int i = 0; i < sm; i++) {
|
||
for (int j = 0; j < sm; j++) {
|
||
double d = cabs(r - qam->constellation[i][j]);
|
||
if (d < min_d) {
|
||
min_d = d;
|
||
i_cl = i;
|
||
j_cl = j;
|
||
}
|
||
}
|
||
}
|
||
|
||
// index du symbole (id) : même mappage que dans bits_to_symbols()
|
||
int id = i_cl * sm + j_cl;
|
||
|
||
for (int b = 0; b < qam->k; b++) {
|
||
bits_hat[k * qam->k + b] = (id >> (qam->k - 1 - b)) & 1;
|
||
}
|
||
}
|
||
}
|
||
|
||
// Libération de la mémoire
|
||
void free_constellation(qam_system* qam) {
|
||
int sm = (int)sqrt(qam->M);
|
||
for (int i = 0; i < sm; i++)
|
||
free(qam->constellation[i]);
|
||
free(qam->constellation);
|
||
}
|
||
|
||
double compare_bits(uint8_t* bits1, uint8_t* bits2, int nb_bits) {
|
||
int errors = 0;
|
||
for (int i = 0; i < nb_bits; i++) {
|
||
if (bits1[i] != bits2[i]) errors++;
|
||
}
|
||
return (double)errors / nb_bits;
|
||
}
|
||
|
||
// ---------- Helpers ----------
|
||
static inline double complex interp_linear(double complex *x, int N, double t) {
|
||
// t: position en échantillons (peut être fractionnaire). On suppose 0 <= t <= N-2
|
||
int i = (int)floor(t);
|
||
double mu = t - (double)i;
|
||
if (i < 0) i = 0;
|
||
if (i >= N-1) return x[N-1];
|
||
return (1.0 - mu) * x[i] + mu * x[i+1];
|
||
}
|
||
|
||
static int nearest_constellation_index(qam_system* qam, double complex z, int *out_i, int *out_j) {
|
||
int sm = (int)sqrt(qam->M);
|
||
double min_d = INFINITY;
|
||
int best_i = 0, best_j = 0;
|
||
for (int i = 0; i < sm; i++) {
|
||
for (int j = 0; j < sm; j++) {
|
||
double d = cabs(z - qam->constellation[i][j]);
|
||
if (d < min_d) {
|
||
min_d = d; best_i = i; best_j = j;
|
||
}
|
||
}
|
||
}
|
||
if (out_i) *out_i = best_i;
|
||
if (out_j) *out_j = best_j;
|
||
return best_i * sm + best_j;
|
||
}
|
||
|
||
// --- Démodulation avec M&M + PLL et écriture dans fichier ---
|
||
void demodulate_real(qam_system* qam, double complex* rx_samples, int total_samples,
|
||
uint8_t* bits_hat, const char* filename)
|
||
{
|
||
double sps = (double) qam->N; // échantillons par symbole
|
||
double Kp_t = 0.005, Ki_t = 0.001;
|
||
double Kp_c = 0.01, Ki_c = 0.01; // PLL porteur
|
||
double timing_integrator = 0.0;
|
||
double phase_integrator = 0.0, phase_est = 0.0;
|
||
double t = sps/2.0;
|
||
|
||
int max_symbols = (int)(total_samples / sps) + 10;
|
||
double complex* syms = malloc(sizeof(double complex) * max_symbols);
|
||
int sym_count = 0;
|
||
|
||
double power = 0.0;
|
||
for(int i = 0; i < total_samples; i++) power += pow(cabs(rx_samples[i]), 2);
|
||
power /= total_samples;
|
||
double agc_gain = 1.0;
|
||
if(power > 0) agc_gain = 1.0 / sqrt(power);
|
||
|
||
// Ouverture du fichier pour écrire les symboles reçus
|
||
FILE* fp = fopen(filename, "w");
|
||
if(!fp) {
|
||
perror("Erreur ouverture fichier .dat");
|
||
free(syms);
|
||
return;
|
||
}
|
||
|
||
while((int)floor(t) + 1 < total_samples) {
|
||
double complex yk = interp_linear(rx_samples, total_samples, t) * agc_gain;
|
||
double complex z = yk * cexp(-I * phase_est);
|
||
syms[sym_count++] = z;
|
||
|
||
// Écriture de chaque symbole reçu dans le fichier
|
||
fprintf(fp, "% .8f % .8f\n", creal(z), cimag(z));
|
||
|
||
if(sym_count >= 3){
|
||
double complex y_k = syms[sym_count-1];
|
||
double complex y_k_1 = syms[sym_count-2];
|
||
double complex y_k_2 = syms[sym_count-3];
|
||
double e_t = creal((y_k - y_k_2) * conj(y_k_1));
|
||
timing_integrator += Ki_t * e_t;
|
||
double timing_adjust = Kp_t * e_t + timing_integrator;
|
||
t += sps + timing_adjust;
|
||
} else t += sps;
|
||
|
||
// PLL carrier
|
||
if(sym_count >= 1) {
|
||
int ii, jj;
|
||
int idx = nearest_constellation_index(qam, z, &ii, &jj);
|
||
double complex d = qam->constellation[ii][jj];
|
||
double e_phase = cimag(z * conj(d));
|
||
phase_integrator += Ki_c * e_phase;
|
||
double phase_adj = Kp_c * e_phase + phase_integrator;
|
||
phase_est += phase_adj;
|
||
if(phase_est > M_PI) phase_est -= 2*M_PI;
|
||
if(phase_est < -M_PI) phase_est += 2*M_PI;
|
||
}
|
||
}
|
||
|
||
// Décision finale et reconstruction bits
|
||
int nb_symbols = sym_count;
|
||
int sm = (int)sqrt(qam->M);
|
||
int nb_bits = nb_symbols * qam->k;
|
||
for(int k = 0; k < nb_symbols; k++) {
|
||
double complex z = syms[k];
|
||
int ii, jj;
|
||
int id = nearest_constellation_index(qam, z, &ii, &jj);
|
||
for(int b = 0; b < qam->k; b++) {
|
||
int bit = (id >> (qam->k - 1 - b)) & 1;
|
||
if(k * qam->k + b < nb_bits) bits_hat[k * qam->k + b] = (uint8_t)bit;
|
||
}
|
||
}
|
||
|
||
fclose(fp);
|
||
free(syms);
|
||
}
|
||
|
||
|
||
int main () {
|
||
qam_system qam;
|
||
qam.M = 16;
|
||
qam.k = (int)log2((double)(qam.M));
|
||
qam.Fs = 44100;
|
||
//qam.Ts = 0.0003;
|
||
//qam.N = (int)(qam.Fs * qam.Ts);
|
||
qam.N = 100;
|
||
qam.Ts = qam.N / qam.Fs;
|
||
qam.Fc = 2000;
|
||
init_constellation(&qam);
|
||
|
||
//int nb_bits = 1000;
|
||
//int nb_symbols = nb_bits / qam.k;
|
||
|
||
//uint8_t* input_bits = malloc(nb_bits * sizeof(uint8_t));
|
||
//for (int i = 0; i < nb_bits; i++) {
|
||
// input_bits[i] = rand() % 2;
|
||
//}
|
||
char* texte = "VVif juge, trempez ce blond whisky aqueuxVif juge, trempez ce blond whisky aqueuxVif juge, trempez ce blond whisky aqueuxVif juge, trempez ce blond whisky aqueuxVif juge, trempez ce blond whisky aqueuxVif juge, trempez ce blond whisky aqueuxVif juge, trempez ce blond whisky aqueuxVif juge, trempez ce blond whisky aqueuxVif juge, trempez ce blond whisky aqueuxVif juge, trempez ce blond whisky aqueuxif juge, trempez ce blond whisky aqueux";
|
||
int nb_chars = strlen(texte);
|
||
int nb_bits = nb_chars * 8;
|
||
int nb_symbols = (nb_bits + qam.k - 1) / qam.k;
|
||
|
||
// Conversion du texte en bits
|
||
uint8_t* input_bits = malloc(nb_bits * sizeof(uint8_t));
|
||
for(int i = 0; i < nb_chars; i++){
|
||
for(int b = 0; b < 8; b++){
|
||
input_bits[i*8 + b] = (texte[i] >> (7-b)) & 1;
|
||
}
|
||
}
|
||
|
||
// Conversion en symboles
|
||
double complex* symbols = malloc(sizeof(double complex) * nb_symbols);
|
||
bits_to_symbols(&qam, input_bits, nb_bits, symbols);
|
||
|
||
// Modulation
|
||
int total_samples = qam.N * nb_symbols;
|
||
double complex* s = malloc(sizeof(double complex) * total_samples);
|
||
modulate(&qam, symbols, nb_symbols, s);
|
||
|
||
//double phase_offset = M_PI / 6.0; // 30 degrés
|
||
//for (int i = 0; i < total_samples; i++) {
|
||
// s[i] *= cexp(I * phase_offset);
|
||
//}
|
||
//double freq_offset = 0; // Hz de décalage
|
||
//for (int i = 0; i < total_samples; i++) {
|
||
// double t = (double)i / qam.Fs;
|
||
// s[i] *= cexp(I * 2 * M_PI * freq_offset * t);
|
||
//}
|
||
//int offset_samples = (int)(0.3 * qam.N); // décalage de 30% d’un symbole
|
||
//memmove(s + offset_samples, s, (total_samples - offset_samples) * sizeof(double complex));
|
||
|
||
|
||
|
||
// Ajout du bruit
|
||
//double snr_dB = 5; // SNR en dB
|
||
//double signal_power = 1.0; // puissance moyenne unitaire après normalisation
|
||
//double snr_lin = pow(10.0, snr_dB / 10.0);
|
||
//double sigma = sqrt(signal_power / (2.0 * snr_lin)); // /2 car bruit sur I et Q
|
||
add_noise(s, total_samples, 0.1);
|
||
|
||
FILE *fp_ref = fopen("constellation_ref.dat", "w");
|
||
int sm = (int)sqrt(qam.M);
|
||
for (int i = 0; i < sm; i++) {
|
||
for (int j = 0; j < sm; j++) {
|
||
fprintf(fp_ref, "% .8f % .8f\n", creal(qam.constellation[i][j]), cimag(qam.constellation[i][j]));
|
||
}
|
||
}
|
||
fclose(fp_ref);
|
||
FILE *fp_constel = fopen("constellation.dat", "w");
|
||
|
||
// Démodulation
|
||
uint8_t* output_bits = (uint8_t*)malloc(nb_bits * sizeof(uint8_t));
|
||
//demodulate(&qam, s, nb_symbols, output_bits, fp_constel);
|
||
for (int i = 0; i < total_samples; i++) {
|
||
s[i] *= cexp(-2 * I * M_PI * qam.Fc * (i / qam.Fs));
|
||
}
|
||
|
||
demodulate_real(&qam, s, total_samples, output_bits, "constellation.dat");
|
||
|
||
|
||
fclose(fp_constel);
|
||
|
||
// Reconstruction du texte
|
||
char* texte_recup = malloc(nb_chars + 1);
|
||
for(int i = 0; i < nb_chars; i++){
|
||
char c = 0;
|
||
for(int b = 0; b < 8; b++){
|
||
c |= output_bits[i*8 + b] << (7-b);
|
||
}
|
||
texte_recup[i] = c;
|
||
}
|
||
texte_recup[nb_chars] = '\0';
|
||
printf("Texte original : %s\n\n", texte);
|
||
printf("Texte demodulé : %s\n", texte_recup);
|
||
|
||
// Calcul du BER
|
||
double ber = compare_bits(input_bits, output_bits, nb_bits);
|
||
printf("Taux d'erreur blind QAM: %.4f\n", ber * 100);
|
||
|
||
// Libération mémoire
|
||
free(input_bits);
|
||
free(output_bits);
|
||
free(symbols);
|
||
free(s);
|
||
free_constellation(&qam);
|
||
|
||
return 0;
|
||
}
|