def pcs_cx(pcs, testing_order, winsize, r, slicing, snr, noise_type): if snr > 100: f = open( "result/cx_testorder%s_hos%d_winsize%d_slice%d_snr%d.csv" % (testing_order, len(pcs), winsize, slicing, snr), 'w') for i in range(r): receive = np.load("temp/data_%d.npy" % (i))[:slicing] temp = cumx(receive, pcs, len(pcs), testing_order, winsize) f.write('%s\n' % temp) print "snr=+inf, ", temp elif noise_type == "white": f = open( "result/cx_testorder%s_hos%d_winsize%d_slice%d_white_snr%d.csv" % (testing_order, len(pcs), winsize, slicing, snr), 'w') for i in range(r): receive = np.load("temp/data_white_%d_%d.npy" % (snr, i))[:slicing] temp = cumx(receive, pcs, len(pcs), testing_order, winsize) f.write('%s\n' % temp) print "white noise, snr=%d, " % (snr), temp elif noise_type == "color": f = open( "result/cx_testorder%s_hos%d_winsize%d_slice%d_color_snr%d.csv" % (testing_order, len(pcs), winsize, slicing, snr), 'w') for i in range(r): receive = np.load("temp/data_color_%d_%d.npy" % (snr, i))[:slicing] temp = cumx(receive, pcs, len(pcs), testing_order, winsize) f.write('%s\n' % temp) print "color noise, snr=%d, " % (snr), temp else: print "ERROR: the noise type is wrong!!" f.close()
def task_cx(pcs, testing_order, winsize, r, slicing, snr, noise_type): if snr > 100: f = open("result/cx_testorder%s_hos%d_winsize%d_slice%d_snr%d.csv"%(testing_order, len(pcs), winsize, slicing, snr), 'w') for i in range(r): receive = np.load("temp/data_%d.npy"%(i))[:slicing] temp = cumx(receive, pcs, len(pcs), testing_order, winsize) f.write('%s\n' % temp) print "snr=+inf, ", temp elif noise_type=="white": f = open("result/cx_testorder%s_hos%d_winsize%d_slice%d_white_snr%d.csv"%(testing_order, len(pcs), winsize, slicing, snr), 'w') for i in range(r): receive = np.load("temp/data_white_%d_%d.npy"%(snr, i))[:slicing] temp = cumx(receive, pcs, len(pcs), testing_order, winsize) f.write('%s\n' % temp) print "white noise, snr=%d, "%(snr), temp elif noise_type=="color": f = open("result/cx_testorder%s_hos%d_winsize%d_slice%d_color_snr%d.csv"%(testing_order, len(pcs), winsize, slicing, snr), 'w') for i in range(r): receive = np.load("temp/data_color_%d_%d.npy"%(snr, i))[:slicing] temp = cumx(receive, pcs, len(pcs), testing_order, winsize) f.write('%s\n' % temp) print "color noise, snr=%d, "%(snr), temp else: print "ERROR: the noise type is wrong!!" f.close()
def maestx(y, pcs, q, norder=3, samp_seg=1, overlap=0): """ MAEST MA parameter estimation via the GM-RCLS algorithm, with Tugnait's fix y - time-series (vector or matrix) q - MA order norder - cumulant-order to use [default = 3] samp_seg - samples per segment for cumulant estimation [default: length of y] overlap - percentage overlap of segments [default = 0] flag - 'biased' or 'unbiased' [default = 'biased'] Return: estimated MA parameter vector """ assert norder >= 2 and norder <= 4, "Cumulant order must be 2, 3, or 4!" nsamp = len(y) overlap = max(0, min(overlap, 99)) c2 = cumx(y, pcs, 2, q, samp_seg, overlap) c2 = np.hstack((c2, np.zeros(q))) cumd = cumx(y, pcs, norder, q, samp_seg, overlap, 0, 0)[::-1] cumq = cumx(y, pcs, norder, q, samp_seg, overlap, q, q) cumd = np.hstack((cumd, np.zeros(q))) cumq[:q] = np.zeros(q) cmat = toeplitz(cumd, np.hstack((cumd[0], np.zeros(q)))) rmat = toeplitz(c2, np.hstack((c2[0], np.zeros(q)))) amat0 = np.hstack((cmat, -rmat[:, 1:q + 1])) rvec0 = c2 cumq = np.hstack((cumq[2 * q:q - 1:-1], np.zeros(q))) cmat4 = toeplitz(cumq, np.hstack((cumq[0], np.zeros(q)))) c3 = cumd[:2 * q + 1] amat0 = np.vstack((np.hstack((amat0, np.zeros((3*q+1,1)))), \ np.hstack((np.hstack((np.zeros((2*q+1,q+1)), cmat4[:,1:q+1])), \ np.reshape(-c3,(len(c3),1)))))) rvec0 = np.hstack((rvec0, -cmat4[:, 0])) row_sel = range(q) + range(2 * q + 1, 3 * q + 1) + range( 3 * q + 1, 4 * q + 1) + range(4 * q + 2, 5 * q + 2) amat0 = amat0[row_sel, :] rvec0 = rvec0[row_sel] bvec = lstsq(amat0, rvec0)[0] b1 = bvec[1:q + 1] / bvec[0] b2 = bvec[q + 1:2 * q + 1] if norder == 3: if all(b2 > 0): b1 = np.sign(b1) * np.sqrt(0.5 * (b1**2 + b2)) else: print 'MAEST: alternative solution b1 used' else: b1 = np.sign(b2) * (abs(b1) + abs(b2)**(1. / 3)) / 2 # if all(np.sign(b2) == np.sign(b1)): # b1 = np.sign(b1)* (abs(b1) + abs(b2)**(1./3))/2 # else: # print 'MAEST: alternative solution b1 used' return np.hstack(([1], b1))
def arma(pcs, ar, ma, winsize, mc_round, slicing, snr, noise_type): """ Input variables: pcs: array-like pcs choices ar, ma: order for the ar and ma models winsize: length of processing segmentation mc_round: number of monte carlo simulations slicing: length of slicing for the generated data snr: snr level for specific data file (larger than 100 for noise free noice_type: white or color. """ if snr > 100: f1 = open("result/ar%s_hos%d_winsize%d_slice%d_snr%d_pcs%s.csv"%(ar, len(pcs), winsize, slicing, snr, ''.join([str(k) for k in pcs]) ), 'w') f2 = open("result/ma%s_hos%d_winsize%d_slice%d_snr%d_pcs%s.csv"%(ma, len(pcs), winsize, slicing, snr, ''.join([str(k) for k in pcs]) ), 'w') for i in range(mc_round): receive = np.load("temp/arma_data_%d.npy"%(i))[:slicing] temp = ar_estimate(receive, pcs, ar, ma, winsize) f1.write('%s\n' % temp) print "AR - snr=+inf, ", temp temp = cumx(receive, pcs, len(pcs), ma, winsize) f2.write('%s\n' % temp) print "MA - snr=+inf, ", temp elif noise_type=="white": f1 = open("result/ar%s_hos%d_winsize%d_slice%d_white_snr%d_pcs%s.csv"%(ar, len(pcs), winsize, slicing, snr, ''.join([str(k) for k in pcs]) ), 'w') f2 = open("result/ma%s_hos%d_winsize%d_slice%d_white_snr%d_pcs%s.csv"%(ma, len(pcs), winsize, slicing, snr, ''.join([str(k) for k in pcs]) ), 'w') for i in range(mc_round): receive = np.load("temp/arma_data_white_%d_%d.npy"%(snr, i))[:slicing] temp = ar_estimate(receive, pcs, ar, ma, winsize) f1.write('%s\n' % temp) print "AR - white noise, snr=%d, "%(snr), temp temp = cumx(receive, pcs, len(pcs), ma, winsize) f2.write('%s\n' % temp) print "MA - white noise, snr=%d, "%(snr), temp elif noise_type=="color": f1 = open("result/ar%s_hos%d_winsize%d_slice%d_color_snr%d_pcs%s.csv"%(ar, len(pcs), winsize, slicing, snr, ''.join([str(k) for k in pcs]) ), 'w') f2 = open("result/ma%s_hos%d_winsize%d_slice%d_color_snr%d_pcs%s.csv"%(ma, len(pcs), winsize, slicing, snr, ''.join([str(k) for k in pcs]) ), 'w') for i in range(mc_round): receive = np.load("temp/arma_data_color_%d_%d.npy"%(snr, i))[:slicing] temp = ar_estimate(receive, pcs, ar, ma, winsize) f1.write('%s\n' % temp) print "AR - color noise, snr=%d, "%(snr), temp temp = cumx(receive, pcs, len(pcs), ma, winsize) f2.write('%s\n' % temp) print "MA - color noise, snr=%d, "%(snr), temp else: print "ERROR: the noise type is wrong!!" f1.close() f2.close()
def task_cx(pcs, taps, winsize, r, slicing): if len(taps) <= 3: file_tag = "short" else: file_tag = "long" f = open("cumulant_test_%s_cx%d_%d_%d_slice%d.csv"%(file_tag, len(pcs), winsize, int(''.join(map(str,pcs))), slicing), 'w') for i in range(r): signal = np.load("/home/work/rsls/data/exp_deviate_one_%d.npy"%(i))[:slicing] receive = ir.moving_average(taps, signal) temp = cx.cumx(receive, pcs, len(pcs), len(taps)-1, winsize) f.write('%s\n' % temp) print temp f.close()
def ar_estimate(sig, pcs, ar, ma, winsize): if len(pcs) != 3: raise ValueError("The ar estimate could only handle 3rd-order cumulant") rb = ma+ar+1 m = np.zeros((rb, 2*ar+ma)) for p in range(rb): temp = cumx(sig, pcs, 3, 2*ar+ma-1, winsize, 0, p-ar) m[p,0] = temp[len(temp)/2] m[p,1:] = (temp[:len(temp)/2][::-1]+temp[len(temp)/2+1:])/2 m = m.T # put the cumulants into algo. matrix result = np.zeros((ar*rb, ar)) for i in range(ar*rb): for j in range(ar): result[i,j] = m[ma+j+1+i/rb, ar-i%rb] _, s, _ = np.linalg.svd(result) return s/s[0]
def ar_estimate(sig, pcs, ar, ma, winsize): if len(pcs) != 3: raise ValueError( "The ar estimate could only handle 3rd-order cumulant") rb = ma + ar + 1 m = np.zeros((rb, 2 * ar + ma)) for p in range(rb): temp = cumx(sig, pcs, 3, 2 * ar + ma - 1, winsize, 0, p - ar) m[p, 0] = temp[len(temp) / 2] m[p, 1:] = (temp[:len(temp) / 2][::-1] + temp[len(temp) / 2 + 1:]) / 2 m = m.T # put the cumulants into algo. matrix result = np.zeros((ar * rb, ar)) for i in range(ar * rb): for j in range(ar): result[i, j] = m[ma + j + 1 + i / rb, ar - i % rb] _, s, _ = np.linalg.svd(result) return s / s[0]
import maest as ma import impulse_response as ir winsize = 512 taps = [1, -2.333, 0.667] signal = np.load("../data/exp_deviate_one_0.npy")[:10000] receive = ir.moving_average(taps, signal) nl = [4, 3, 4] print "With nested sampling:", ncx.cumx(receive, nl, len(nl), len(taps) - 1, winsize) signal = np.load("../data/exp_deviate_one_0.npy")[:10000] receive = ir.moving_average(taps, signal) pcs = [2, 3, 5] print "Without downsampling:", cx.cumx(receive, pcs, len(pcs), len(taps) - 1, winsize) signal = np.load("../data/exp_deviate_one_0.npy")[:10000] receive = ir.moving_average(taps, signal) pcs = [1, 1, 1] print "With PCS downsampling:", cx.cumx(receive, pcs, len(pcs), len(taps) - 1, winsize) ####################### signal = np.load("../data/exp_deviate_one_0.npy")[:10000] receive = ir.moving_average(taps, signal) pcs = [4, 3, 4] print "With nested sampling:", nma.maestx(receive, pcs, len(taps) - 1, len(pcs), winsize)
def arma(pcs, ar, ma, winsize, mc_round, slicing, snr, noise_type): """ Input variables: pcs: array-like pcs choices ar, ma: order for the ar and ma models winsize: length of processing segmentation mc_round: number of monte carlo simulations slicing: length of slicing for the generated data snr: snr level for specific data file (larger than 100 for noise free noice_type: white or color. """ if snr > 100: f1 = open( "result/ar%s_hos%d_winsize%d_slice%d_snr%d_pcs%s.csv" % (ar, len(pcs), winsize, slicing, snr, ''.join( [str(k) for k in pcs])), 'w') f2 = open( "result/ma%s_hos%d_winsize%d_slice%d_snr%d_pcs%s.csv" % (ma, len(pcs), winsize, slicing, snr, ''.join( [str(k) for k in pcs])), 'w') for i in range(mc_round): receive = np.load("temp/arma_data_%d.npy" % (i))[:slicing] temp = ar_estimate(receive, pcs, ar, ma, winsize) f1.write('%s\n' % temp) print "AR - snr=+inf, ", temp temp = cumx(receive, pcs, len(pcs), ma, winsize) f2.write('%s\n' % temp) print "MA - snr=+inf, ", temp elif noise_type == "white": f1 = open( "result/ar%s_hos%d_winsize%d_slice%d_white_snr%d_pcs%s.csv" % (ar, len(pcs), winsize, slicing, snr, ''.join( [str(k) for k in pcs])), 'w') f2 = open( "result/ma%s_hos%d_winsize%d_slice%d_white_snr%d_pcs%s.csv" % (ma, len(pcs), winsize, slicing, snr, ''.join( [str(k) for k in pcs])), 'w') for i in range(mc_round): receive = np.load("temp/arma_data_white_%d_%d.npy" % (snr, i))[:slicing] temp = ar_estimate(receive, pcs, ar, ma, winsize) f1.write('%s\n' % temp) print "AR - white noise, snr=%d, " % (snr), temp temp = cumx(receive, pcs, len(pcs), ma, winsize) f2.write('%s\n' % temp) print "MA - white noise, snr=%d, " % (snr), temp elif noise_type == "color": f1 = open( "result/ar%s_hos%d_winsize%d_slice%d_color_snr%d_pcs%s.csv" % (ar, len(pcs), winsize, slicing, snr, ''.join( [str(k) for k in pcs])), 'w') f2 = open( "result/ma%s_hos%d_winsize%d_slice%d_color_snr%d_pcs%s.csv" % (ma, len(pcs), winsize, slicing, snr, ''.join( [str(k) for k in pcs])), 'w') for i in range(mc_round): receive = np.load("temp/arma_data_color_%d_%d.npy" % (snr, i))[:slicing] temp = ar_estimate(receive, pcs, ar, ma, winsize) f1.write('%s\n' % temp) print "AR - color noise, snr=%d, " % (snr), temp temp = cumx(receive, pcs, len(pcs), ma, winsize) f2.write('%s\n' % temp) print "MA - color noise, snr=%d, " % (snr), temp else: print "ERROR: the noise type is wrong!!" f1.close() f2.close()
import nested_maest as nma import maest as ma import impulse_response as ir winsize = 512 taps = [1, -2.333, 0.667] signal = np.load("../data/exp_deviate_one_0.npy")[:10000] receive = ir.moving_average(taps, signal) nl = [4,3,4] print "With nested sampling:", ncx.cumx(receive, nl, len(nl), len(taps)-1, winsize) signal = np.load("../data/exp_deviate_one_0.npy")[:10000] receive = ir.moving_average(taps, signal) pcs = [2,3,5] print "Without downsampling:", cx.cumx(receive, pcs, len(pcs), len(taps)-1, winsize) signal = np.load("../data/exp_deviate_one_0.npy")[:10000] receive = ir.moving_average(taps, signal) pcs = [1,1,1] print "With PCS downsampling:", cx.cumx(receive, pcs, len(pcs), len(taps)-1, winsize) ####################### signal = np.load("../data/exp_deviate_one_0.npy")[:10000] receive = ir.moving_average(taps, signal) pcs = [4,3,4] print "With nested sampling:", nma.maestx (receive, pcs, len(taps)-1, len(pcs), winsize) signal = np.load("../data/exp_deviate_one_0.npy")[:10000] receive = ir.moving_average(taps, signal)