-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQPSK.py
More file actions
158 lines (136 loc) · 3.53 KB
/
Copy pathQPSK.py
File metadata and controls
158 lines (136 loc) · 3.53 KB
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
# -*- coding: utf-8 -*-
##
## Author: Emil Svendsen
## Date: 20/11-2018
## Last edit: 20/11-2018
import scipy.signal as signal
import scipy.fftpack as fft
import numpy as np
import matplotlib.pyplot as plt
## Random sequence lenght of Nbits
Nbits = 8
a = 2 * np.random.randint(0, 2, Nbits) -1
b = 2 * np.random.randint(0, 2, Nbits) -1
## Pulse length
T = 1
## Sampling periode
Ts = 1e-4
## Sampling frequency
fs = 1 / Ts
## Makes 1D array with length of (T / Ts)
g = np.ones(int(T / Ts))
## Makes one big matrix size (g x a)
x = np.outer(g, a)
## Make it into one big 1D array
x = np.matrix.flatten(x, order='F')
## Makes one big matrix size (g x b)
y = np.outer(g, b)
## Make it into one big 1D array
y = np.matrix.flatten(y, order='F')
## Number of samples
Nsamples = (T / Ts) * Nbits
## Time vector
t = np.arange(0, (Nsamples) * Ts, Ts)
## Carrier frequency
fc = 2000
## Carrier waves
cosfc = np.sqrt(2) * np.cos(2 * np.pi * fc * t)
sinfc = np.sqrt(2) * np.sin(2 * np.pi * fc * t)
## Element wise multiply and add
v = np.add(np.multiply(x, cosfc), np.multiply(y, sinfc))
## Frequency definition # Sample rate / Nsamples
df = fs / Nsamples
## Only positive frequencies
f = np.arange(0, (df * Nsamples / 2), df)
## Only negative frequencies
fNeg = np.multiply(np.fliplr([np.arange(0+df, (df * Nsamples / 2) + df, df)]), -1)
## All frequencies
f = np.concatenate((fNeg, f), axis = None)
##
i = np.multiply(v, cosfc)
q = np.multiply(v, sinfc)
hWin = signal.firwin(0.5, 100,)
## lowpass hamming filter coeficients
##hWin = [0.01496783, 0.2, 0.01496783]
test1 = signal.lfilter(hWin, 1, i)
test2 = signal.lfilter(hWin, 1, q)
## Plot
figXY = plt.figure()
plt.title('x and y plot')
## x plot
plt.subplot(5,2,1)
plt.plot(t, x, 'b')
plt.ylabel('x', color='b')
plt.xlabel('Tid [s]')
plt.grid()
plt.axis('tight')
## y plot'
plt.subplot(5,2,2)
plt.plot(t, y, 'g')
plt.ylabel('y', color='g')
plt.xlabel('Tid [s]')
plt.grid()
plt.axis('tight')
## x frequency plot
plt.subplot(5,2,3)
plt.plot(f, fft.fftshift(np.abs(fft.fft(x))**2) / Nsamples**2 , 'b')
plt.ylabel('y effekt', color='b')
plt.xlabel('Frekvens [Hz]')
plt.grid()
plt.axis('tight')
#plt.xlim((-5,5))
## y frequency plot
plt.subplot(5,2,4)
plt.plot(f, fft.fftshift(np.abs(fft.fft(y))**2) / Nsamples**2 , 'g')
plt.ylabel('y effekt', color='g')
plt.xlabel('Frekvens [Hz]')
plt.grid()
plt.axis('tight')
#plt.xlim((-5,5))
## v frequency plot
plt.subplot(5,2,5)
plt.plot(f, fft.fftshift(np.abs(fft.fft(v))**2) / Nsamples**2 , 'g')
plt.ylabel('v effekt', color='g')
plt.xlabel('Frekvens [Hz]')
plt.grid()
plt.axis('tight')
## v frequency plot
plt.subplot(5,2,6)
plt.plot(t, v, 'g')
plt.ylabel('v effekt', color='g')
plt.xlabel('Frekvens [Hz]')
plt.grid()
plt.axis('tight')
## i frequency plot
plt.subplot(5,2,7)
plt.plot(f, fft.fftshift(np.abs(fft.fft(i))**2) / Nsamples**2 , 'g')
plt.ylabel('i effekt', color='g')
plt.xlabel('Frekvens [Hz]')
plt.grid()
plt.axis('tight')
#plt.xlim((-5,5))
## q frequency plot
plt.subplot(5,2,8)
plt.plot(f, fft.fftshift(np.abs(fft.fft(q))**2) / Nsamples**2 , 'g')
plt.ylabel('q effekt', color='g')
plt.xlabel('Frekvens [Hz]')
plt.grid()
plt.axis('tight')
#plt.xlim((-5,5))
## i frequency plot
plt.subplot(5,2,9)
plt.plot(t, test1[np.arange(0, (Nsamples), dtype=int)] , 'g')
plt.ylabel('i effekt', color='g')
plt.xlabel('Frekvens [Hz]')
plt.grid()
plt.axis('tight')
#plt.xlim((-5,5))
## q frequency plot
plt.subplot(5,2,10)
plt.plot(t, test2[np.arange(0, (Nsamples), dtype=int)], 'g')
plt.ylabel('q effekt', color='g')
plt.xlabel('Frekvens [Hz]')
plt.grid()
plt.axis('tight')
#plt.xlim((-5,5))
plt.show()