Gray Mapping
This commit is contained in:
60
QAM/qam.c
60
QAM/qam.c
@ -14,27 +14,23 @@ struct qam_system_s {
|
||||
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
|
||||
double complex* constellation; // Tableau de symboles I + j Q 1D
|
||||
};
|
||||
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);
|
||||
}
|
||||
qam->constellation = (double complex*)malloc(sizeof(double complex) * qam->M);
|
||||
|
||||
double norm_factor = sqrt((double)(qam->M - 1) / 3.0); // Pour puissance unitaire
|
||||
|
||||
for (int i = 0; i < sm; i++) {
|
||||
double complex ip = -(sm - 1) + 2 * i;
|
||||
double 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;
|
||||
double qp = -(sm - 1) + 2 * j;
|
||||
int idx = i * sm + j;
|
||||
qam->constellation[idx] = (ip + I * qp) / norm_factor;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -81,27 +77,15 @@ void demodulate(qam_system* qam, double complex* s, int nb_symbols, uint8_t* bit
|
||||
double min_d = INFINITY;
|
||||
int i_cl = 0;
|
||||
int 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;
|
||||
}
|
||||
for (int idx = 0; idx < qam->M; idx++) {
|
||||
double d = cabs(r - qam->constellation[idx]);
|
||||
if (d < min_d) {
|
||||
min_d = d;
|
||||
i_cl = idx / sm;
|
||||
j_cl = idx % sm;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
// Ancienne methode (non gray)
|
||||
// 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;
|
||||
}
|
||||
*/
|
||||
|
||||
// Gray mapping
|
||||
if (qam->k % 2 != 0) {
|
||||
printf("demodulate : k pair (k = %d)\n", qam->k);
|
||||
@ -141,20 +125,6 @@ void add_noise (double complex* s, int len, double sigma) {
|
||||
}
|
||||
|
||||
// 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];
|
||||
}
|
||||
}*/
|
||||
|
||||
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 = (int)sqrt(qam->M);
|
||||
@ -182,7 +152,7 @@ void bits_to_symbols (qam_system* qam, uint8_t* bits, int nb_bits, double comple
|
||||
printf("bits_to_symbols : IOOR i_gray = %d j_gray = %d sm = %d \n", i_gray, j_gray, sm);
|
||||
exit(1);
|
||||
}
|
||||
symbols[sym] = qam->constellation[i_gray][j_gray];
|
||||
symbols[sym] = qam->constellation[i_gray * sm + j_gray];
|
||||
}
|
||||
}
|
||||
|
||||
@ -211,7 +181,8 @@ void fill_constellation_data(qam_system* qam, FILE *fp_ref) {
|
||||
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]));
|
||||
int idx = i * sm + j;
|
||||
fprintf(fp_ref, "% .8f % .8f\n", creal(qam->constellation[idx]), cimag(qam->constellation[idx]));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -241,6 +212,7 @@ void free_constellation(qam_system* qam) {
|
||||
for (int i = 0; i < sm; i++)
|
||||
free(qam->constellation[i]);
|
||||
free(qam->constellation);
|
||||
|
||||
}
|
||||
|
||||
int main () {
|
||||
|
||||
Reference in New Issue
Block a user