learnComm

79 days ago by cardmaster

convolution([1,5,6], [3,2]) 
       
[3, 17, 28, 12]
[3, 17, 28, 12]
 
       
(0.79999999999999982, 1.0, 0.0)
(0.79999999999999982, 1.0, 0.0)
#Plot a Lissajous Curve (http://en.wikipedia.org/wiki/Lissajous_curve) def parametricFuncs(f): def x(t): return cos(2*pi*t) def y(t): return sin(2*f*pi*t) return (x, y) N = 5 g_lissajours = [parametric_plot((parametricFuncs(f)), (0, 1.0), color=hue(f * (0.99 / N)))for f in range(1, N)] comb = None for g in g_lissajours: if (comb == None): comb = g else: comb = comb + g comb.show(aspect_ratio = 1.0) 
       
#QPSK - a simuation of QPSK moduling def randomStepVal(amp=1.0): if (random() >= 0.5): return amp else: return 0 #given a list of tuple, and generate a piecewise function def PiecewiseWithConsts(lst): #Gather all const values, and generate a dict for it constFuncs = dict() for pair in lst: if (not pair[1] in constFuncs): constval = pair[1] constFunc(x) = constval constFuncs[pair[1]] = constFunc funclist = [[(lst[0][0], lst[0][0]), constFuncs[lst[0][1]]]] for pair in lst: oldpair = funclist[-1][0]; funclist[-1][0] = (oldpair[0], pair[0]) funclist.append([(pair[0], pair[0] + 1), constFuncs[pair[1]]]) return Piecewise(funclist) def randomStepSignal(T): valdict = dict() def f_stepSig(t): smpT = floor(t / T) * T if (smpT in valdict): return valdict[smpT]; else: valdict[smpT] = randomStepVal() return valdict[smpT] return f_stepSig #IQ moduling def module_IQ(mapped, w): (f_I, f_Q) = mapped def f_IQMod(t): return f_I(t) * cos (w * t) - f_Q(t) * sin(w*t) return f_IQMod def mapBoolToValue(boolExp, trueValue, falseValue): if (boolExp): return trueValue else: return falseValue def xor(a, b): return int(a).__xor__(int(b)) def combSignalBits(sig, period, t, nBits): val = 0; sampT = floor(t / (period * nBits)) * (period * nBits) + period / 2.0 for i in range(nBits): val = val | (Integer(sig(sampT + period * i)) << i) return val def IQMapper_ByList(valMap, indexer): def f_I(t): return valMap[indexer(t)][0] def f_Q(t): return valMap[indexer(t)][1] return (f_I, f_Q) #The QPSK mapper, give a signal function and it's period, return a #The mapper using Gray Code to arrange values def IQMapper_qpsk(sig, period): amp = 1 / sqrt(2) valueMap = [(amp, amp), (-amp, amp), (-amp, -amp), (amp, -amp)] def combValue(t): return combSignalBits(sig, period, t, 2) return IQMapper_ByList(valueMap, combValue) def IQMapper_8psk(sig, period): C = cos(pi / 8) S = sin(pi / 8) valueMap = [(C, S), (S, C), (-S, C), (-C, S), (-C, -S), (-S, -C), (S, -C), (C, -S)] def combValue(t): return combSignalBits(sig, period, t, 3) return IQMapper_ByList(valueMap, combValue) def IQMapper_qam(sig, period): A = 1.0 / sqrt(2.0) A3 = A * 3.0 valueMap = [(A3, A3), (A, A3), (-A, A3), (-A3, A3), (-A3, A), (-A, A), (A, A), (A3, A), (A3, -A), (A, -A), (-A, -A), (-A3, -A), (-A3, -A3), (-A, -A3), (A, -A3), (A3, -A3)] def combValue(t): return combSignalBits(sig, period, t, 4) return IQMapper_ByList(valueMap, combValue) def moduleSignal(sig, period, mapper, moduler, w = 0): def sig_wrap(t): return sig(t) mapped = mapper(sig_wrap, period) if (w == 0): w = 2 * pi / period; moduled = moduler(mapped, w) lst = [sig_wrap] for mp in mapped: lst.append(mp) lst.append(moduled) return lst T = 0.5 count = 64 sig = randomStepSignal(T) plotFuncs = moduleSignal(sig, T, IQMapper_qpsk, module_IQ) plotRange = (0.0, T * count) plotFuncs += moduleSignal(sig, T, IQMapper_8psk, module_IQ) plotFuncs += moduleSignal(sig, T, IQMapper_qam, module_IQ) graphs = [plot(func, plotRange) for func in plotFuncs] for g in graphs: g.show(aspect_ratio = 1.0) 
       






















sinc(x) = sin(pi * x) / (pi * x) plot(sinc, x, -10, 10).show(ymin=-1) 
       
 
       
 
       
#Create a Band pass filter def createBandFilter(f, B, Q = 1.0): h(t) = sinc(2 * B * t) * cos(2 * pi * Q * B * t) / (1 - (4 * Q * B * t)^2) return h h = createBandFilter(0, 0.5, 0.9) plot (h, (-3.0, 3.0), aspect_ratio = 1.0) 
       
funcs = moduleSignal(sig, T, IQMapper_qpsk, module_IQ) graphs = [plot(func, (0.0, T * 32)) for func in funcs] for g in graphs: g.show(aspect_ratio = 1.0) 
       






A = Matrix([[1 * exp(2 * pi * i), -2], [3, 4]]) w = vector([1, 2 * i]) w * A 
       
(6*I + 1, 8*I - 2)
(6*I + 1, 8*I - 2)
w * 0.5 
       
(0.500000000000000, 1.00000000000000*I)
(0.500000000000000, 1.00000000000000*I)
len(w) 
       
2
2
range(10) 
       
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#generate a n * n transform matrix for DFT operation def fourierTransformMatrix(n): matlist = [] for j in range(n): linelst = [] for k in range(n): linelst.append(exp(- (i * 2 * pi * j * k) / n)) matlist.append(linelst) return Matrix(matlist) #Input time domain list of values, return Freq Domain list def dft(numlist): vec = vector([num for num in numlist]) n = len(vec) return [CC(val / n) for val in (vec * fourierTransformMatrix(n))] def phase(complexNum): ccnum = CC(complexNum) absval = abs(ccnum) if (absval > 0.00001): return acos(ccnum.real() / absval) else: return 0.0 
       
freq = 50 #50Hz min frequency wave, T = 0.01s sig(t) = cos((4 * freq) * (2 * pi * t)) + 3 * sin((2 * freq) * (2 * pi * t)) + 2 * cos( (3 *freq) * (2 * pi * t)) + sin(freq * (2 * pi * t)) + cos(freq * (2 * pi * t)) maxFreq = freq * 4 smpInterval = 1 / maxFreq / 2.5 #0.01s per sample nSamples = 50 #smpInterval = 1.0 / freq / nSamples #Sample only one period samples = [sig(x * smpInterval) for x in range(nSamples)] timeDomain = list_plot(samples) freqVec = dft(samples) freqDomainAmp = list_plot([abs(fd) for fd in freqVec], hue=0.1) freqDomainPhase = list_plot([phase(fd) for fd in freqVec], hue = 0.4) show(timeDomain, aspect_ratio = 3) show(freqDomainAmp, legend_title="Amp", aspect_ratio = 5) show(freqDomainPhase, legend_title="Phase", aspect_ratio = 5) 
       




exp(- i * pi / 2) 
       
-I
-I
phase(CC(1 + 2 * i)) 
       
Traceback (click to the left of this block for traceback)
...
NameError: name 'phase' is not defined
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_31.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("cGhhc2UoQ0MoMSArIDIgKiBpKSk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/tmp/tmp_o4Phy/___code___.py", line 3, in <module>
    exec compile(u'phase(CC(_sage_const_1  + _sage_const_2  * i))
  File "", line 1, in <module>
    
NameError: name 'phase' is not defined
help(phase) 
       
Traceback (click to the left of this block for traceback)
...
NameError: name 'phase' is not defined
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_34.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("aGVscChwaGFzZSk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/tmp/tmpMWgfIL/___code___.py", line 2, in <module>
    exec compile(u'help(phase)
  File "", line 1, in <module>
    
NameError: name 'phase' is not defined