-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodulation.c
321 lines (272 loc) · 11.9 KB
/
modulation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "modulation.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
/**
* Funkcja moduluje ciąg danych
*
* @param CLTUs ciąg bloków CLTU
* @return modulatedData zmodulowany ciąg danych
*/
double complex* qpskModulation(unsigned int* CLTUs, int length) {
double complex* modulatedData = malloc(sizeof(double complex) * 32 * length / 2); //jedna liczba zespolona na każde dwa bity danych
unsigned int firstFlag = 1;
unsigned int secondFlag = 1;
int complexCounter = 0;
secondFlag = secondFlag << 1;
//
const double real = cos(angle);
const double imaginary = sin(angle);
double complex offsetComplex = real + I * imaginary;
//
for (int i = 0; i < length ; ++i) {
for (int j = 0; j < 16; ++j) {
if ((CLTUs[i] & firstFlag) == 0 && (CLTUs[i] & secondFlag) == 0) {
modulatedData[complexCounter] = ConstelationPoint0;
} else if ((CLTUs[i] & firstFlag) != 0 && (CLTUs[i] & secondFlag) == 0) {
modulatedData[complexCounter] = ConstelationPoint1;
} else if ((CLTUs[i] & firstFlag) == 0 && (CLTUs[i] & secondFlag) != 0) {
modulatedData[complexCounter] = ConstelationPoint2;
} else {
modulatedData[complexCounter] = ConstelationPoint3;
}
// zmiana probek jako symulacja zaklocen w kanale transmisji
//modulatedData[complexCounter] /= 2;
modulatedData[complexCounter] *= offsetComplex;
firstFlag = firstFlag << 2;
secondFlag = secondFlag << 2;
complexCounter++;
}
firstFlag = 1;
secondFlag = 1;
secondFlag = secondFlag << 1;
}
return modulatedData;
}
/**
* Funkcja demoduluje ciąg danych
*
* @param CLTUs zmidulowany ciąg danych
* @return zdemodulowany ciąg danych
*/
unsigned int* qpskDemodulation(double complex* data, int length) {
const double real = cos(angle);
const double imaginary = sin(angle * -1);
double complex offsetComplex = real + I * imaginary;
float numberOfInts =(float) length / 32;
int parsedNumberOfInts = numberOfInts;
if (numberOfInts > parsedNumberOfInts) parsedNumberOfInts += 1;
unsigned int* demodulatedData = malloc(sizeof(complex double) * parsedNumberOfInts );
// shifting values
for (int i = 0; i < length; ++i) {
//printf("%f %f | ", creal(data[i]), cimag(data[i]));
data[i] = data[i] * offsetComplex;
//printf("%f %f || ", creal(data[i]), cimag(data[i]));
}
double sum = 0;
for (int i = 0; i < numberOfSamplesToComputeAmplitudeFactor; ++i) {
sum += 1 / sqrt(creal(data[i]) * creal(data[i]) + cimag(data[i]) * cimag(data[i]));
}
double amplitudeFactor = sum / numberOfSamplesToComputeAmplitudeFactor;
//printf("\n %f \n", amplitudeFactor);
unsigned int demodulatedValue = 0;
int counter = 0;
unsigned int constelationPoint0Value = 0; // bitowo 00
unsigned int constelationPoint1Value = 1; // bitowo 01
unsigned int constelationPoint2Value = 2; // bitowo 10
unsigned int constelationPoint3Value = 3; // bitowo 11
// scale amplitude to 1
for (int i = 0; i < length; ++i) {
data[i] *= amplitudeFactor;
//printf("%f %f || \n", creal(data[i]), cimag(data[i]));
if (i != 0 && (i % 16) == 0) {
demodulatedData[counter++] = demodulatedValue;
demodulatedValue = 0;
constelationPoint0Value = 0;
constelationPoint1Value = 1;
constelationPoint2Value = 2;
constelationPoint3Value = 3;
}
// pierwsza cwiartka ukladu
if (creal(data[i]) > 0 && cimag(data[i]) > 0) {
demodulatedValue = demodulatedValue | constelationPoint0Value;
} else if (creal(data[i]) > 0 && cimag(data[i]) < 0) {
demodulatedValue = demodulatedValue | constelationPoint1Value;
} else if (creal(data[i]) < 0 && cimag(data[i]) > 0) {
demodulatedValue = demodulatedValue | constelationPoint2Value;
} else if (creal(data[i]) < 0 && cimag(data[i]) < 0) {
demodulatedValue = demodulatedValue | constelationPoint3Value;
}
constelationPoint0Value = constelationPoint0Value << 2;
constelationPoint1Value = constelationPoint1Value << 2;
constelationPoint2Value = constelationPoint2Value << 2;
constelationPoint3Value = constelationPoint3Value << 2;
}
demodulatedData[counter] = demodulatedValue;
return demodulatedData;
}
int countNumberOfSamplesPerPeriod() {
float numberOfSamples = (float) (subcarrierFreq + modulatedSignalFactor * bitrate) * 2 / subcarrierFreq;
int numberOfSamplesInt = (int) numberOfSamples;
if (numberOfSamples > numberOfSamplesInt) numberOfSamplesInt++;
return numberOfSamplesInt;
}
double countPhaseOffset(double complex* modulatedData, int length) {
double sum = 0;
for (int i = 0; i < length; ++i) {
sum += cimag(modulatedData[i]);
}
double phaseOffset = sum / length;
return phaseOffset;
}
double complex computeComplexSignalValue(double amplitude, double angle) {
const double real = cos(2* PI + angle * amplitude);
const double imaginary = sin(2* PI + angle * amplitude);
return real + I * imaginary;
}
double complex* shiftModulatedData(double complex* modulatedData, double offset, int length) {
double complex* shiftedData = malloc(sizeof(double complex) * length);
const double real = cos(offset) * 1;
const double imaginary = sin(offset) * 1;
double complex offsetComplex = real + I * imaginary;
for (int i = 0; i < length; ++i) {
shiftedData[i] = modulatedData[i] * offsetComplex;
}
return shiftedData;
// druga FUNKCJA DO obliczania ilosci probek na okres
int countNumberOfSamplesPerPeriod() {
float numberOfSamples = (subcarrierFreq + modulatedSignalFactor * bitrate) * 2;
int numberOfSamplesInt = numberOfSamples;
if (numberOfSamples > numberOfSamplesInt) numberOfSamplesInt++;
return numberOfSamplesInt;
}
double* getSamplesValues(int numberOfSamplesPerPeriod) {
double* samplesAmplitude = malloc(sizeof(double) * numberOfSamplesPerPeriod);
double x = 360 / numberOfSamplesPerPeriod;
double val = PI / 180;
for (int i = 0; i < numberOfSamplesPerPeriod; ++i) {
samplesAmplitude[i] = sin(i * x * val);
}
return samplesAmplitude;
}
double complex* oversampling(double complex* modulatedData, int length) {
double complex* oversampledData = malloc(sizeof(double complex) * length * oversamplingLevel);
for(int i = 0; i < length; ++i) {
oversampledData[i * oversamplingLevel] = modulatedData[i];
for (int j = 1; j < oversamplingLevel; ++j) {
oversampledData[i * oversamplingLevel + j] = 0 + 0*I;
}
}
return oversampledData;
}
double complex* filtering(double complex* data, int length) {
double complex* filteredData = malloc(sizeof(double complex) * length * oversamplingLevel);
int counter = 0;
double realValue = 0;
double imagValue = 0;
for (int i = 0; i < numberOfFiltersValues; ++i) {
for (int j = 0; j <= i; ++j) {
realValue += filterValues[j] * creal(data[j]);
imagValue += filterValues[j] * cimag(data[j]);
}
filteredData[counter++] = realValue + imagValue * I;
realValue = 0;
imagValue = 0;
}
for (int i = numberOfFiltersValues; i < length * oversamplingLevel; ++i) {
for (int j = 0; j < numberOfFiltersValues; ++j) {
double value =(double) 1 / 2;
realValue += filterValues[j] * creal(data[i + j]);
imagValue += filterValues[j] * cimag(data[i + j]);
}
filteredData[counter++] = realValue + imagValue * I;
realValue = 0;
imagValue = 0;
}
return filteredData;
}
double complex* removeOversampledValues(double complex * data, int length) {
double complex* filteredData = malloc(sizeof(double complex) * length);
int counter = 0;
for (int i = 0; i < length * oversamplingLevel; i += oversamplingLevel) {
filteredData[counter++] = data[i];
}
return filteredData;
}
double complex* analogPhaseModulation(unsigned int* CLTUs, int length) {
const int numberOfSamplesPerPeriod = countNumberOfSamplesPerPeriod();
double* samplesValues = getSamplesValues(numberOfSamplesPerPeriod);
const int outputDataLength = length * numberOfSamplesPerPeriod * 32;
double complex* outputData = malloc(sizeof(double complex) * outputDataLength);
int outputDataCounter = 0;
unsigned int flag = 1;
double deviation = angleDeviation;
double complex tmpOffset = cos(PI / 180 * 20) + sin(PI / 180 * 20) * I;
for (int i = 0; i < length; ++i) {
for (int j = 0; j < 32; ++j) {
if ((CLTUs[i] & flag) == 0) {
for (int sampleNumber = 0; sampleNumber < numberOfSamplesPerPeriod; ++sampleNumber) {
double tmp = samplesValues[sampleNumber];
double complex tmp1 = computeComplexSignalValue(samplesValues[sampleNumber], deviation) * -1;
outputData[outputDataCounter++] = tmp1;// computeComplexSignalValue(samplesValues[sampleNumber], deviation) * -1;
deviation *= -1;
}
} else {
for (int sampleNumber = 0; sampleNumber < numberOfSamplesPerPeriod; ++sampleNumber) {
double tmp = samplesValues[sampleNumber];
double complex tmp1 = computeComplexSignalValue(samplesValues[sampleNumber], deviation);
outputData[outputDataCounter++] = tmp1;// computeComplexSignalValue(samplesValues[sampleNumber], deviation);
deviation *= -1;
}
}
flag = flag << 1;
}
flag = 1;
}
return outputData;
}
unsigned int* analogPhaseDemodulation(double complex* modulatedData, int modulatedDataBytesNumber, int numberOfComplexNumbers) {
const int numberOfSamplesPerPeriod = countNumberOfSamplesPerPeriod();
double phaseOffset = countPhaseOffset(modulatedData, numberOfSamplesPerPeriod * numberOfSymbolToCalculatePhaseOffset);
double complex* shiftedData = shiftModulatedData(modulatedData, phaseOffset, numberOfComplexNumbers * 2);
double complex* filteredData = removeOversampledValues(filtering(shiftedData, numberOfComplexNumbers), numberOfComplexNumbers);
double* samplesValues = getSamplesValues(numberOfSamplesPerPeriod);
unsigned int* demodulatedData = malloc(sizeof(unsigned int) * modulatedDataBytesNumber);
int counter = 0;
unsigned int demodulatedByteValue = 0;
unsigned int bit0Value = 0;
unsigned int bit1Value = 1;
int demodulatedDataCounter = 0;
double periodAmplitudeSum = 0;
double deviation = angleDeviation;
for (int i = 0; i < numberOfComplexNumbers; i += numberOfSamplesPerPeriod) {
for (int j = 0; j < numberOfSamplesPerPeriod; ++j) {
int modulatedDataIndex = i + j;
double demodulatedValue;
demodulatedValue = cimag(filteredData[modulatedDataIndex]) / sin(deviation);
periodAmplitudeSum += demodulatedValue * samplesValues[j];
deviation *= -1;
}
if (periodAmplitudeSum / numberOfSamplesPerPeriod > 0) {
demodulatedByteValue = demodulatedByteValue | bit1Value;
} else {
demodulatedByteValue = demodulatedByteValue | bit0Value;
}
periodAmplitudeSum = 0;
bit1Value = bit1Value << 1;
counter++;
if (counter == 32 ) {
demodulatedData[demodulatedDataCounter++] = demodulatedByteValue;
counter = 0;
demodulatedByteValue = 0;
bit1Value = 1;
}
}
demodulatedData[demodulatedDataCounter] = demodulatedByteValue;
return demodulatedData;
}
void printComplex(double complex* array, int length) {
for (int i = 0; i < length; ++i) {
printf("%f %f |", creal(array[i]), cimag(array[i]));
}
}