QAM add constellation plot
This commit is contained in:
60
QAM/qam.c
60
QAM/qam.c
@ -3,9 +3,11 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <complex.h>
|
||||
#include "../WAV/wav.h"
|
||||
#include "../wav/wav.h"
|
||||
#include "../files/files.h"
|
||||
#include "../plot/plot_constellation.h"
|
||||
#include <string.h>
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#define A 10000
|
||||
|
||||
@ -73,7 +75,7 @@ void bits_to_symbols (qam_system* qam, uint8_t* bits, int nb_bits, double comple
|
||||
}
|
||||
}
|
||||
|
||||
// Modulation qam
|
||||
// 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];
|
||||
@ -83,8 +85,8 @@ void modulate (qam_system* qam, double complex* symbols, int nb_symbols, double
|
||||
}
|
||||
}
|
||||
|
||||
// Demodulation qam
|
||||
void demodulate(qam_system* qam, double complex* s, int nb_symbols, uint8_t* bits_hat, double sigma) {
|
||||
// Demodulation QAM
|
||||
void demodulate(qam_system* qam, double complex* s, int nb_symbols, uint8_t* bits_hat) {
|
||||
for (int k = 0; k < nb_symbols; k++) {
|
||||
double complex r = 0;
|
||||
for (int n = 0; n < qam->N; n++) {
|
||||
@ -128,8 +130,21 @@ void demodulate(qam_system* qam, double complex* s, int nb_symbols, uint8_t* bit
|
||||
}
|
||||
}
|
||||
|
||||
double complex* demodulate_points(qam_system* qam, double complex* s, int nb_symbols) {
|
||||
double complex* points = malloc(sizeof(double complex) * nb_symbols);
|
||||
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 /= qam->N;
|
||||
points[k] = r;
|
||||
}
|
||||
return points;
|
||||
}
|
||||
|
||||
// Libération de la mémoire
|
||||
void free_constellation(qam_system *qam) {
|
||||
void free_constellation(qam_system* qam) {
|
||||
int sm = (int)sqrt(qam->M);
|
||||
for (int i = 0; i < sm; i++)
|
||||
free(qam->constellation[i]);
|
||||
@ -143,50 +158,46 @@ int main (int argc, char *argv[]) {
|
||||
}
|
||||
|
||||
qam_system qam;
|
||||
qam.M = 256;
|
||||
qam.M = 64;
|
||||
qam.k = (int)log2((double)(qam.M));
|
||||
qam.Fs = 44100;
|
||||
qam.Ts = 0.003;
|
||||
qam.Ts = 0.0003;
|
||||
qam.N = (int)qam.Fs * qam.Ts;
|
||||
qam.Fc = 2000;
|
||||
init_constellation(&qam);
|
||||
|
||||
// Nombre de bit multiple de k sinon remplir de zero jusqu'a ce que ce le soit
|
||||
//int bits[16] = {1,0,1,1, 0,1,1,0, 1,1,0,0, 0,0,0,1};
|
||||
//int nb_bits = 16;
|
||||
//int nb_symbols = 16 / qam.k;
|
||||
|
||||
printf("Lecture du fichier\n");
|
||||
printf("Lecture du fichier...\n");
|
||||
// Lecture du fichier et conversion en bits
|
||||
const char *input_filename = argv[1];
|
||||
bit_array input_bits = file_to_bits(input_filename);
|
||||
size_t nb_symbols = input_bits.nb_bits / qam.k;
|
||||
|
||||
printf("Mise en forme des symboles\n");
|
||||
printf("Mise en forme des symboles...\n");
|
||||
// Mise en forme des symboles
|
||||
double complex *symbols = malloc(sizeof(double complex) * nb_symbols);
|
||||
bits_to_symbols(&qam, input_bits.bits, input_bits.nb_bits, symbols);
|
||||
|
||||
printf("Modulation\n");
|
||||
printf("Modulation...\n");
|
||||
// Modulation QAM
|
||||
int total_samples = qam.N * nb_symbols;
|
||||
double complex* s = (double complex*)malloc(sizeof(double complex) * total_samples);
|
||||
modulate(&qam, symbols, nb_symbols, s);
|
||||
|
||||
// Ajout du bruit
|
||||
double signal_power = (2.0/3.0)*(qam.M-1); // puissance moyenne avant échelle
|
||||
double snr_dB = -27; // Signal to noise ratio
|
||||
double snr_lin = pow(10.0, snr_dB / 10.0);
|
||||
double sigma = sqrt(signal_power / snr_lin);
|
||||
printf("Ajout du bruit \n puissance du signal : %f\n SNR db : %f\n sigma : %f\n", signal_power, snr_dB, sigma);
|
||||
add_noise(s, total_samples, sigma);
|
||||
printf("Ajout du bruit... \n puissance du signal : %f\n SNR db : %f\n sigma : %f\n", signal_power, snr_dB, sigma);
|
||||
add_noise(s, total_samples, 1875);
|
||||
|
||||
printf("Demodulation\n");
|
||||
printf("Demodulation...\n");
|
||||
|
||||
// Demodulation QAM
|
||||
bit_array output_bits;
|
||||
output_bits.nb_bits = input_bits.nb_bits;
|
||||
output_bits.bits = (uint8_t*)malloc(output_bits.nb_bits);
|
||||
demodulate(&qam, s, nb_symbols, output_bits.bits, 0.0);
|
||||
demodulate(&qam, s, nb_symbols, output_bits.bits);
|
||||
|
||||
printf("Ecriture...\n");
|
||||
// Ecriture du fichier de Demodulation
|
||||
@ -199,9 +210,18 @@ int main (int argc, char *argv[]) {
|
||||
for (int i = 0; i < total_samples; i++) {
|
||||
si[i] = cimag(s[i]);
|
||||
}
|
||||
write_wav("s.wav", si, total_samples);
|
||||
write_wav("output.wav", si, total_samples);
|
||||
|
||||
// Plot
|
||||
printf("Ploting...");
|
||||
plot_t plot;
|
||||
plot_init(&plot, 1400, 1400, 0.04);
|
||||
plot_draw_constellation(&plot, qam.constellation, qam.M);
|
||||
SDL_Color red = {255, 0, 0, 255};
|
||||
plot_draw_points_animated(&plot, demodulate_points(&qam, s, nb_symbols), nb_symbols, red, 5);
|
||||
|
||||
// Libération mémoire
|
||||
plot_close(&plot);
|
||||
free_bit_array(&input_bits);
|
||||
free_bit_array(&output_bits);
|
||||
free(symbols);
|
||||
|
||||
Reference in New Issue
Block a user