mi@0: """ mi@0: Set of util functions for the section similarity project. mi@0: """ mi@0: mi@0: import copy mi@0: import numpy as np mi@0: import json mi@0: import scipy.fftpack mi@0: import pylab as plt mi@0: mi@0: def resample_mx(X, incolpos, outcolpos): mi@0: """ mi@0: Y = resample_mx(X, incolpos, outcolpos) mi@0: X is taken as a set of columns, each starting at 'time' mi@0: colpos, and continuing until the start of the next column. mi@0: Y is a similar matrix, with time boundaries defined by mi@0: outcolpos. Each column of Y is a duration-weighted average of mi@0: the overlapping columns of X. mi@0: 2010-04-14 Dan Ellis dpwe@ee.columbia.edu based on samplemx/beatavg mi@0: -> python: TBM, 2011-11-05, TESTED mi@0: """ mi@0: noutcols = len(outcolpos) mi@0: Y = np.zeros((X.shape[0], noutcols)) mi@0: # assign 'end times' to final columns mi@0: if outcolpos.max() > incolpos.max(): mi@0: incolpos = np.concatenate([incolpos,[outcolpos.max()]]) mi@0: X = np.concatenate([X, X[:,-1].reshape(X.shape[0],1)], axis=1) mi@0: outcolpos = np.concatenate([outcolpos, [outcolpos[-1]]]) mi@0: # durations (default weights) of input columns) mi@0: incoldurs = np.concatenate([np.diff(incolpos), [1]]) mi@0: mi@0: for c in range(noutcols): mi@0: firstincol = np.where(incolpos <= outcolpos[c])[0][-1] mi@0: firstincolnext = np.where(incolpos < outcolpos[c+1])[0][-1] mi@0: lastincol = max(firstincol,firstincolnext) mi@0: # default weights mi@0: wts = copy.deepcopy(incoldurs[firstincol:lastincol+1]) mi@0: # now fix up by partial overlap at ends mi@0: if len(wts) > 1: mi@0: wts[0] = wts[0] - (outcolpos[c] - incolpos[firstincol]) mi@0: wts[-1] = wts[-1] - (incolpos[lastincol+1] - outcolpos[c+1]) mi@0: wts = wts * 1. /sum(wts) mi@0: Y[:,c] = np.dot(X[:,firstincol:lastincol+1], wts) mi@0: # done mi@0: return Y mi@0: mi@0: def magnitude(X): mi@0: """Magnitude of a complex matrix.""" mi@0: r = np.real(X) mi@0: i = np.imag(X) mi@0: return np.sqrt(r * r + i * i); mi@0: mi@0: def json_to_bounds(segments_json): mi@0: """Extracts the boundaries from a json file and puts them into mi@0: an np array.""" mi@0: f = open(segments_json) mi@0: segments = json.load(f)["segments"] mi@0: bounds = [] mi@0: for segment in segments: mi@0: bounds.append(segment["start"]) mi@0: bounds.append(bounds[-1] + segments[-1]["duration"]) # Add last boundary mi@0: f.close() mi@0: return np.asarray(bounds) mi@0: mi@0: def json_bounds_to_bounds(bounds_json): mi@0: """Extracts the boundaries from a bounds json file and puts them into mi@0: an np array.""" mi@0: f = open(bounds_json) mi@0: segments = json.load(f)["bounds"] mi@0: bounds = [] mi@0: for segment in segments: mi@0: bounds.append(segment["start"]) mi@0: f.close() mi@0: return np.asarray(bounds) mi@0: mi@0: def json_to_labels(segments_json): mi@0: """Extracts the labels from a json file and puts them into mi@0: an np array.""" mi@0: f = open(segments_json) mi@0: segments = json.load(f)["segments"] mi@0: labels = [] mi@0: str_labels = [] mi@0: for segment in segments: mi@0: if not segment["label"] in str_labels: mi@0: str_labels.append(segment["label"]) mi@0: labels.append(len(str_labels)-1) mi@0: else: mi@0: label_idx = np.where(np.asarray(str_labels) == segment["label"])[0][0] mi@0: labels.append(label_idx) mi@0: f.close() mi@0: return np.asarray(labels) mi@0: mi@0: def json_to_beats(beats_json_file): mi@0: """Extracts the beats from the beats_json_file and puts them into mi@0: an np array.""" mi@0: f = open(beats_json_file, "r") mi@0: beats_json = json.load(f) mi@0: beats = [] mi@0: for beat in beats_json["beats"]: mi@0: beats.append(beat["start"]) mi@0: f.close() mi@0: return np.asarray(beats) mi@0: mi@0: def analyze_results(file): mi@0: f = open(file, "r") mi@0: lines = f.readlines() mi@0: F = [] mi@0: for line in lines: mi@0: F.append(float(line.split("\t")[0])) mi@0: f.close() mi@0: print np.mean(F) mi@0: mi@0: def compute_ffmc2d(X): mi@0: """Computes the 2D-Fourier Magnitude Coefficients.""" mi@0: # 2d-fft mi@0: fft2 = scipy.fftpack.fft2(X) mi@0: mi@0: # Magnitude mi@0: fft2m = magnitude(fft2) mi@0: mi@0: # FFTshift and flatten mi@0: fftshift = scipy.fftpack.fftshift(fft2m).flatten() mi@0: mi@0: #cmap = plt.cm.get_cmap('hot') mi@0: #plt.imshow(np.log1p(scipy.fftpack.fftshift(fft2m)).T, interpolation="nearest", mi@0: # aspect="auto", cmap=cmap) mi@0: #plt.show() mi@0: mi@0: # Take out redundant components mi@0: return fftshift[:fftshift.shape[0]/2+1]