def dft(drawable: Line, scale=False): result = trunc(DiscreteFourierTransform("dft of " + drawable.title, drawable), values=int(drawable.getN() / 2)) if scale: result.x = scale_array(result.x, right=0.5) return result
def read_from_wav(self, filepath): rate, data = wavfile.read(INPUT_FOLDER + filepath) self.last_rate = rate x = np.zeros(len(data)) for i in range(1, len(data)): x[i] = x[i - 1] + 1 / rate return rate, Line(filepath, x=x, y=data)
def lesson4(): xcr = io.read_from_xcr('h400x300.xcr') sample_line = Line("line", y=xcr.matrix[100]) bs_line = bsf_line(sample_line, lowcut=100, highcut=150) bs_pic = bsf_pic(xcr, lowcut=100, highcut=150) return np.array([(xcr, auto(sample_line)), (xcr, dft(auto(sample_line), scale=True)), (bs_pic, auto(bs_line)), (bs_pic, dft(auto(bs_line), scale=True))])
def cdf(picture, normalise=True): hist = np.array(histogram(picture).y) for i in range(1, hist.size): hist[i] += hist[i - 1] if normalise: cdf_m = np.ma.masked_equal(hist, 0) cdf_m = (cdf_m - cdf_m.min()) * 255 / (cdf_m.max() - cdf_m.min()) hist = np.ma.filled(cdf_m, 0).astype('uint8') # m = hist.max() # for i in range(1, hist.size): # hist[i] = int(hist[i] / m * 255) return Line('CDF of ' + picture.title, y=hist)
def base(self): multiplier = 2 * math.pi * self.f * self.dt y = create_array( self.getN(), lambda i: math.sin(multiplier * i) * math.exp( -self.k * self.dt * i)) return Line("", x=self.x, y=y)
def mult_const(drawable, const): return Line(drawable.title, y=drawable.y * const)
def read_from_csv(self, filepath): df = pd.read_csv(INPUT_FOLDER + filepath) data = df['Open'] return Line(filepath, N=len(data), y=data)
def read_from_dat(self, filepath): with open(INPUT_FOLDER + filepath, 'rb') as input_file: array_from_file = input_file.read() format = '{:d}f'.format(len(array_from_file) // 4) array_from_file = np.array(struct.unpack(format, array_from_file)) return Line(title=filepath, N=len(array_from_file), y=array_from_file)
def trunc(drawable, values): return Line(drawable.title, N=values, y=drawable.y[:values])
def delta(interval=200, n=N): y = create_array( n, lambda i: 1 if i % interval == 0 else 0, ) return Line("", y=y)
def deconv(d1: Line, d2: Line): return Line("Deconv of " + d1.title + " and " + d2.title, y=_deconv(d1.y, d2.y))
def lowpass(n=128, dt=0.001, fCut=50): return Line("Low Pass Filter", y=low_pass_filter(n, dt, fCut))
def fft(drawable): return absolute( trunc(Line("Spectrum", y=np.fft.fft(drawable.y)), values=int(drawable.getN() / 2)))
def delta(self): y = create_array( self.getN(), lambda i: 1 if i % self.interval == 0 else 0, ) return Line("", x=self.x, y=y)
def diff(line: Line): return Line("diff of " + line.title, y=np.diff(line.y.astype("int32")))
def absolute(drawable): return Line(drawable.title, y=np.abs(drawable.y))
def sub(drawable, start, end=None): end = drawable.getN() if end is None else end return Line(drawable.title, N=end - start, y=drawable.y[start:end])
def bsf_line(line: Line, lowcut, highcut): y = _bsf(line.y, lowcut, highcut, fs=len(line.y)) return Line(title="bsf of " + line.title, y=y)
def bandpass(n=128, dt=0.001, fCutLower=50, fCutUpper=100): return Line("Band Pass Filter", y=band_pass_filter(n, dt, fCutLower, fCutUpper))
def bandstop(n=128, dt=0.001, fCutLower=50, fCutUpper=100): return Line("Band Stop Filter", y=band_stop_filter(n, dt, fCutLower, fCutUpper))
def highpass(n=128, dt=0.001, fCut=50): return Line("High Pass Filter", y=high_pass_filter(n, dt, fCut))
def histogram(picture: Picture): hist = np.zeros(256) for pixel in np.nditer(picture.matrix, op_flags=['readwrite']): hist[pixel] += 1 return Line('histogram of ' + picture.title, y=hist)
def build_y(wiener, value): y = wiener.calculateY()[offset:] y = y - y[0] + value print(self.get_description(Line("", y=y))) return y
def reg_deconv(d1: Line, d2: Line, k): return Line("Deconv of " + d1.title + " and " + d2.title, y=_regularized_deconv(d1.y, d2.y, k))
def halflowpass(n=128, dt=0.001, fCut=50): return Line("Half Low Pass Filter", y=half_low_pass_filter(n, dt, fCut))
def get_line(self, index): return Line("Pic line" + str(index), y=self.matrix[index])
def readFromFile(self, filename): x, y = np.loadtxt(INPUT_FOLDER + filename, delimiter=DELIMITER, unpack=True) return Line(filename[3:-len(DATA_FORMAT)], x=x, y=y)
def hpf_line(line: Line, cut): y = _hpf(line.y, cut, fs=len(line.y)) return Line(title="lpf of " + line.title, y=y)
def base(f=10, k=15, dt=0.005, n=N): multiplier = 2 * math.pi * f * dt y = create_array( n, lambda i: math.sin(multiplier * i) * math.exp(-k * dt * i)) return Line("", y=y)