def average_sequence(sequences): res = Sequence() for x in sequences[0].x: res._seq[x] = 0 for seq in sequences: res._seq[x] += seq._seq[x] res._seq[x] /= len(sequences) return res
def gist(seq, intervals=10): from collections import defaultdict res = defaultdict(int) mi = min(seq.y) ma = max(seq.y) size = (ma - mi) / intervals for y in seq.y: idx = (y - mi) // size res[idx] += 1 return Sequence.from_func(range(intervals), lambda x: res[x])
def high_pass_filter(size, dt, fc): lpf = Filter.low_pass_filter(size, dt, fc) lpw = lpf.y hpf = [] for i in range(2 * size + 1): if i == size: hpf.append(1 - lpw[i]) else: hpf.append(-lpw[i]) return Sequence.from_func(range(2 * size + 1), lambda x: hpf[x])
def _band_filter(size, dt, fc1, fc2): lpf_low = Filter.low_pass_filter(size, dt, fc1).y lpf_up = Filter.low_pass_filter(size, dt, fc2).y for i in range(2 * size + 1): if i == size: lpf_low[i] = 1 + lpf_low[i] - lpf_up[i] else: lpf_low[i] = lpf_low[i] - lpf_up[i] return Sequence.from_func(range(2 * size + 1), lambda x: lpf_low[x])
def idft(complex_pairs): res = [] n = len(complex_pairs) for k in range(n): sum = 0 for t in range(n): angle = (2 * math.pi * k * t) / n sum += complex_pairs[t][0] * math.cos( angle) + complex_pairs[t][1] * math.sin(angle) res.append(sum) return Sequence.from_func(range(n), lambda x: res[x])
def auto_correlation(seq, start=0, end=None): avg = Analysis.avg(seq, start=start, end=end) var = Analysis.var(seq, start=start, end=end) res = OrderedDict() y = seq.y if not end: end = len(seq._seq) for shift in range(start, end): res[shift - start] = sum([(y[x] - avg) * (y[x + shift] - avg) for x in range(0, end - shift)]) / var return Sequence.from_dict( OrderedDict(zip(seq.x[start:end], res.values())))
def cross_correlation(first, second): if (len(first) != len(second)): raise ValueError("Sequences are not the same size") first_avg = Analysis.avg(first) second_avg = Analysis.avg(second) div = math.sqrt(Analysis.var(first) + Analysis.var(second)) res = [] f_y = first.y s_y = second.y for shift in range(len(first)): res.append( sum([(f_y[k] - first_avg) * (s_y[k + shift] - second_avg) for k in range(len(first) - shift)]) / div) return Sequence.from_dict(OrderedDict(zip(first.x, res)))
def dtf(seq): n = len(seq) y_tmp = seq.y res = [] for k in range(n): Re = 0 Im = 0 for t in range(n): angle = 2 * math.pi * t * k / n Re += y_tmp[t] * math.cos(angle) Im += y_tmp[t] * math.sin(angle) res.append(math.sqrt((Re / n)**2 + (Im / n)**2)) return Sequence.from_dict( OrderedDict(zip(seq.x[:n // 2:], res[:n // 2])))
def convolution(first, second): one = first.y two = second.y n = len(first) m = len(second) conv = [] for i in range(n): if i in range(n + m): res = 0 for j in range(m): if (i - j) in range(0, n): res += one[i - j] * two[j] conv.append(res) else: conv.append(0) return Sequence.from_func(range(n), lambda x: conv[x])
def dtf(seq): n = len(seq) y_tmp = seq.y x_tmp = seq.x res = [] for k in range(n): Re = 0 Im = 0 for t in range(n): angle = 2 * math.pi * t * k / n Re += y_tmp[t] * math.cos(angle) Im += y_tmp[t] * math.sin(angle) res.append(math.sqrt((Re / n)**2 + (Im / n)**2)) freq_top = 1 / (2 * (x_tmp[1] - x_tmp[0])) freq_delta = 2 * freq_top / n x_tmp = [freq_delta * x for x in range(len(seq.x[:n // 2]))] return Sequence.from_dict(OrderedDict(zip(x_tmp, res[:n // 2])))
def convolve(a, v): c = np.convolve(a.y, v.y) return Sequence.from_func(range(len(v), len(a)), lambda x: c[x])
def low_pass_filter(size, dt, fc): half = Filter._half_filter(size, dt, fc) result = [*list(reversed(half)), *half[1:]] return Sequence.from_func(range(2 * size + 1), lambda x: result[x])
def untrend(seq, window_size=5): res = OrderedDict() for idx, x in enumerate(seq.x): res[x] = Analysis.avg(seq, max(0, idx - window_size), min(len(seq) - 1, idx + window_size)) return seq - Sequence.from_dict(res)
def delta(dots, interval): return Sequence.from_func(range(dots), lambda x: 1 if x % interval == 0 else 0)
def base(dots, freq, dt, relaxation): mult = 2 * math.pi * freq * dt return Sequence.from_func( range(dots), lambda x: math.sin(mult * x) * math.exp(-relaxation * dt * x))