def execute(self, arff_data, attributes, X): base_width = np.array(arff_data['data'][:, attributes.index('baseWidth')], dtype='float64') target_width = np.array(arff_data['data'][:, attributes.index('targetWidth')], dtype='float64') base_height = np.array(arff_data['data'][:, attributes.index('baseHeight')], dtype='float64') target_height = np.array(arff_data['data'][:, attributes.index('targetHeight')], dtype='float64') base_viewport = np.array(arff_data['data'][:, attributes.index('baseViewportWidth')], dtype='float64') target_viewport = np.array(arff_data['data'][:, attributes.index('targetViewportWidth')], dtype='float64') X.append(np.abs((base_width - target_width)/np.maximum(np.abs(base_viewport - target_viewport), 1))) X.append(np.abs((base_height - target_height)/np.maximum(np.maximum(base_height, target_height), 1))) arff_data['features'] = arff_data['features'] + ['width_comp', 'height_comp'] return X
def execute(self, arff_data): attributes = [attribute[0] for attribute in arff_data['attributes']] dataset = arff_data['data'] X = (arff_data['X'].T.tolist() if 'X' in arff_data else []) base_height = np.array(dataset[:, attributes.index('baseHeight')], dtype='float64') target_height = np.array(dataset[:, attributes.index('targetHeight')], dtype='float64') base_width = np.array(dataset[:, attributes.index('baseWidth')], dtype='float64') target_width = np.array(dataset[:, attributes.index('targetWidth')], dtype='float64') X.append( np.minimum(base_height * base_width, target_height * target_width)) base_x = np.array(dataset[:, attributes.index('baseX')], dtype='float64') target_x = np.array(dataset[:, attributes.index('targetX')], dtype='float64') base_y = np.array(dataset[:, attributes.index('baseY')], dtype='float64') target_y = np.array(dataset[:, attributes.index('targetY')], dtype='float64') X.append( np.sqrt( np.power(np.abs(base_x - target_x), 2) + np.power(np.abs(base_y - target_y), 2))) X.append( np.abs((base_height * base_width) - (target_height * target_width)) / np.maximum( np.minimum(base_height * base_width, target_height * target_width), np.ones(len(base_height)))) X.append(dataset[:, attributes.index('chiSquared')]) arff_data['X'] = np.array(X, dtype='float64').T prev_features = (arff_data['features'] if 'features' in arff_data else []) arff_data['features'] = prev_features + [ 'area', 'displacement', 'sdr', 'chisquared' ] arff_data['y'] = np.array( arff_data['data'][:, attributes.index(self._class_attr)], dtype='int16') return arff_data
def train(self, X, y, learning_rate = 0.01, update_size = None): assert len(np.shape(X)) == 2, "Invalid input shape. The input must be 2d (batch, input_dim)" batch_size, input_dim = np.shape(X) assert self.input_dim == input_dim, "Unmatch input_dim." X = np.column_stack((X, np.ones((batch_size, 1)))) y = y.reshape((-1, self.output_dim)) if self.updateMethod == "closed-form": # check whether the matrix can be inversed assert np.linalg.det(X) != 0, "This matrix cann't be inversed." # update the parameters self.w = np.linalg.inv(np.dot(X.T, X)) self.w = np.dot(self.w, X.T) self.w = np.dot(self.w, y) # predict under the train set Y_predict = np.dot(X, self.w) # calculate the absolute differences loss_train = np.average(np.abs(Y_predict - y)) return Y_predict, loss_train
def spec(windowSize, stepSize, input, fftMag, sr): windowSize = int(windowSize) stepSize = int(stepSize) numberOfSteps = int(math.floor((len(input) - windowSize) / stepSize) + 1) spectrogram = np.zeros((int(fftMag / 2), numberOfSteps)) timeArray = np.zeros((numberOfSteps)) for i in range(numberOfSteps): currentWindow = input[stepSize * i:(stepSize * i) + windowSize] transformedWindow = scipy.fft.fft(currentWindow, n=fftMag) truncatedWindow = transformedWindow[:int(fftMag / 2)] spectrogram[:, i] = np.log(np.abs(truncatedWindow) + 1) timeArray[i] = (stepSize * i + (stepSize * i + windowSize)) / (2 * sr) return spectrogram, timeArray
def rotate_bound(image, angle): # grab the dimensions of the image and then determine the # center (h, w) = image.shape[:2] (cX, cY) = (w // 2, h // 2) # grab the rotation matrix (applying the negative of the # angle to rotate clockwise), then grab the sine and cosine # (i.e., the rotation components of the matrix) M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0) cos = np.abs(M[0, 0]) sin = np.abs(M[0, 1]) # compute the new bounding dimensions of the image nW = int((h * sin) + (w * cos)) nH = int((h * cos) + (w * sin)) # adjust the rotation matrix to take into account translation M[0, 2] += (nW / 2) - cX M[1, 2] += (nH / 2) - cY # perform the actual rotation and return the image return cv2.warpAffine(image, M, (nW, nH))
def execute(self, arff_data, attributes, X): base_width = np.array(arff_data['data'][:, attributes.index('baseWidth')], dtype='float64') target_width = np.array(arff_data['data'][:, attributes.index('targetWidth')], dtype='float64') base_viewport = np.array(arff_data['data'][:, attributes.index('baseViewportWidth')], dtype='float64') target_viewport = np.array(arff_data['data'][:, attributes.index('targetViewportWidth')], dtype='float64') base_left = np.array(arff_data['data'][:, attributes.index('baseX')], dtype='float64') base_right = np.array(base_viewport - (base_left + base_width), dtype='float64') target_left = np.array(arff_data['data'][:, attributes.index('targetX')], dtype='float64') target_right = np.array(target_viewport - (target_left + target_width), dtype='float64') X.append(np.abs((base_left - target_left) / np.maximum(np.abs(base_viewport - target_viewport), 1))) X.append(np.abs((base_right - target_right) / np.maximum(np.abs(base_viewport - target_viewport), 1))) base_y = np.array(arff_data['data'][:, attributes.index('baseY')], dtype='float64') target_y = np.array(arff_data['data'][:, attributes.index('targetY')], dtype='float64') X.append(np.abs((base_y - target_y) / np.maximum(np.abs(base_viewport - target_viewport), 1))) arff_data['features'] = arff_data['features'] + ['left_comp', 'right_comp', 'y_comp'] return X
def execute(self, arff_data, attributes, X): nrows = len(X) if len(X) > 0 else 1 base_x = np.array(arff_data['data'][:, attributes.index('baseX')], dtype='float64') base_y = np.array(arff_data['data'][:, attributes.index('baseY')], dtype='float64') base_height = np.array(arff_data['data'][:, attributes.index('baseHeight')], dtype='float64') base_width = np.array(arff_data['data'][:, attributes.index('baseWidth')], dtype='float64') target_x = np.array(arff_data['data'][:, attributes.index('targetX')], dtype='float64') target_y = np.array(arff_data['data'][:, attributes.index('targetY')], dtype='float64') target_height = np.array(arff_data['data'][:, attributes.index('targetHeight')], dtype='float64') target_width = np.array(arff_data['data'][:, attributes.index('targetWidth')], dtype='float64') base_parent_x = np.array(arff_data['data'][:, attributes.index('baseParentX')], dtype='float64') base_parent_y = np.array(arff_data['data'][:, attributes.index('baseParentY')], dtype='float64') target_parent_x = np.array(arff_data['data'][:, attributes.index('targetParentX')], dtype='float64') target_parent_y = np.array(arff_data['data'][:, attributes.index('targetParentY')], dtype='float64') base_p_sibling_x = np.array(arff_data['data'][:, attributes.index('basePreviousSiblingLeft')], dtype='float64') base_p_sibling_y = np.array(arff_data['data'][:, attributes.index('basePreviousSiblingTop')], dtype='float64') target_p_sibling_x = np.array(arff_data['data'][:, attributes.index('targetPreviousSiblingLeft')], dtype='float64') target_p_sibling_y = np.array(arff_data['data'][:, attributes.index('targetPreviousSiblingTop')], dtype='float64') base_n_sibling_x = np.array(arff_data['data'][:, attributes.index('baseNextSiblingLeft')], dtype='float64') base_n_sibling_y = np.array(arff_data['data'][:, attributes.index('baseNextSiblingTop')], dtype='float64') target_n_sibling_x = np.array(arff_data['data'][:, attributes.index('targetNextSiblingLeft')], dtype='float64') target_n_sibling_y = np.array(arff_data['data'][:, attributes.index('targetNextSiblingTop')], dtype='float64') X.append(np.abs((base_x - base_parent_x) - (target_x - target_parent_x)) / np.minimum(target_width, base_width)) X.append(np.abs((base_y - base_parent_y) - (target_y - target_parent_y)) / np.minimum(target_height, base_height)) X.append(np.abs((base_x - base_p_sibling_x) - (target_x - target_p_sibling_x)) / np.minimum(target_width, base_width)) X.append(np.abs((base_y - base_p_sibling_y) - (target_y - target_p_sibling_y)) / np.minimum(target_height, base_height)) X.append(np.abs((base_x - base_n_sibling_x) - (target_x - target_n_sibling_x)) / np.minimum(target_width, base_width)) X.append(np.abs((base_y - base_n_sibling_y) - (target_y - target_n_sibling_y)) / np.minimum(target_height, base_height)) arff_data['features'] = arff_data['features'] + ['parent_x', 'parent_y', 'previous_sibling_x', 'previous_sibling_y', 'next_sibling_x', 'next_sibling_y'] return X
return Y_predict, loss_train elif self.updateMethod.lower == "sgd": if update_size == None: update_size = batch_size assert update_size <= batch_size, "Update size lager than batch size!" y_predict = np.dot(X, self.w) diff = y_predict - y randind = np.random.randint(0,X.shape[0]-1, size=update_size) # calculate the gradient G = -np.dot(X[randind].T.reshape(-1, update_size), y[randind].reshape(update_size, -1)) G += np.dot(X[randind].T.reshape(-1, update_size), X[randind].reshape(update_size, -1)).dot(self.w) G = -G # update the parameters self.w += learning_rate * G y_predict_selected = np.dot(X[randind], self.w) loss_train = np.average(np.abs(y_predict_selected - y[randind])) return y_predict, loss_train def predict(self, X): assert len(np.shape(X)) == 2, "Invalid input shape. The input must be 2d (batch, input_dim)" batch_size, input_dim = np.shape(X) assert self.input_dim == input_dim, "Unmatch input_dim." X = np.column_stack((X, np.ones((batch_size, 1)))) y_predict = np.dot(X, self.w) return y_predict
def wavFileToSound(wavFile): y, sr = librosa.load(wavFile) shiftedData = np.abs(librosa.stft(y)) return shiftedData
def decode(self): display_result = '' filtedData = [] current_length = 0 current_data = [] data = [] flag = 0 impulse_fft = [] impulse_fft_tmp = [] bin = [] real = [] self.decode_text.setPlainText(''.join(real)) bandpass1 = 3000 bandpass2 = 7000 read_signal, fs = sf.read(FILENAME) wn1 = 2.0 * bandpass1 / sampling_rate wn2 = 2.0 * bandpass2 / sampling_rate b, a = signal.butter(8, [wn1, wn2], 'bandpass') # 범위를 조금 더 넓게 해야할 filtedData = signal.filtfilt(b, a, read_signal) # data为要过滤的信号 current_length = len(filtedData) current_data = filtedData while 1: once_check = 0 corr = [] corr_index = dict() print('finding preamble') print('current_data length', len(current_data)) for i in range(current_length - len(preamble_y)): corr.append( np.corrcoef(current_data[i:i + len(preamble_y)], preamble_y)[0, 1]) if once_check + 24000 == i and once_check != 0: print('corr 찾는거 ') break if corr[i] > 0.5: if once_check == 0: once_check = i print('once_check', once_check) corr_index[i] = corr[i] try: flag = max(corr_index.items(), key=operator.itemgetter(1))[0] except: print('decode 结束') break print(flag) data = current_data[flag + len(preamble_y):flag + len(preamble_y) + 60000] target_fre = 6000 n = len(data) window = 600 impulse_fft = np.zeros(n) for i in range(int(n - window)): y = np.fft.fft(data[i:i + int(window) - 1]) y = np.abs(y) index_impulse = round(target_fre / sampling_rate * window) impulse_fft[i] = max(y[index_impulse - 2:index_impulse + 2]) sliding_window = 5 impulse_fft_tmp = impulse_fft for i in range(1 + sliding_window, n - sliding_window): impulse_fft_tmp[i] = np.mean(impulse_fft[i - sliding_window:i + sliding_window]) impulse_fft = impulse_fft_tmp # # # position_impulse = []; # half_window = 800; # # # # for i in range(n-half_window*2): # if impulse_fft[i+half_window] > 90 and impulse_fft[i+half_window] == max(impulse_fft[i - half_window: i + half_window]): # position_impulse.append(i) # message_bin = np.zeros(230400) # for i in range(len(position_impulse)): # message_bin[math.ceil(position_impulse / 4800)] = 1 # real_message_start = 1 # last_one_index = 1 # for i in range(3): # if message_bin[i] == 1: # last_one_index = i # # real_message_start = last_one_index + 1 # # real_message_bin = message_bin[real_message_start:230400] # # curr_package_index = 0 # curr_bin_index = 1 # real_message_bin = np.matrix.H(real_message_bin) plus = 0 adjust = 0 count = 0 while 1: decode_length = '' if adjust == 1: plus += 0.1 print(plus) for i in range(8): bin = np.mean(impulse_fft[i * 1200:(i + 1) * 1200]) bin += plus print(bin) if bin < 5: decode_length = decode_length + '0' else: decode_length = decode_length + '1' print(decode_length) decode_payload_length = int(decode_length, 2) count += 1 if count == 40: break if decode_payload_length != 35: adjust = 1 else: break if count == 40: decode_length = '' for i in range(8): bin = np.mean(impulse_fft[i * 1200:(i + 1) * 1200]) print(bin) if bin < 3: decode_length = decode_length + '0' else: decode_length = decode_length + '1' print(decode_length) decode_payload_length = int(decode_length, 2) adjust = 0 decode_payload = '' for i in range(decode_payload_length): bin = np.mean(impulse_fft[(i + 8) * 1200:(i + 1 + 8) * 1200]) if bin < 3: decode_payload = decode_payload + '0' else: decode_payload = decode_payload + '1' print(bin) else: decode_payload = '' for i in range(decode_payload_length): bin = np.mean(impulse_fft[(i + 8) * 1200:(i + 1 + 8) * 1200]) if adjust == 1: bin += plus if bin < 5: decode_payload = decode_payload + '0' else: decode_payload = decode_payload + '1' print(bin) print(decode_payload) while 1: if len(decode_payload) % 7 != 0: decode_payload = decode_payload + '0' else: break print(1200 * (int(decode_length, 2) + 8)) current_data = current_data[1200 * (int(decode_length, 2) + 8 + 20) + flag:len(current_data)] current_length = len(current_data) display_result = display_result + decode_payload real = [] for i in range(int(len(display_result) / 7)): real.append(decode_c(display_result[i * 7:(i + 1) * 7])) print(real) self.decode_text.setPlainText(''.join(real)) print('result:', ''.join(real)) global start cost_time = "time:" + str(time.time() - start) + '\n' decode_payload = decode_payload + '\n' file = open("result_translate.txt", 'w') file.write(cost_time) file.write(decode_payload) file.write(''.join(real)) file.close()
def meanNonDiag(whitenedMatrix): whitenedCov = np.abs(np.cov(whitenedMatrix)) sumNonDiag = np.sum(whitenedCov) - np.trace(whitenedCov) n = whitenedCov.shape[0] return sumNonDiag / (n * n - n)
"""Pre Processing for audio files as they are input. For training data. """ #from __future__ import print_function import librosa.display import librosa import np # 1. Get the file path to the included audio example data_folder = "C:\\Users\\Reece\\Documents\\GitHub\\ALD\\ALD\\src\\" filename = data_folder + "50FrenchClean.mp3" filename = librosa.util.example_audio_file() # 2. Load the audio as a waveform `y` # Store the sampling rate as `sr` y, sr = librosa.load(filename) # 3. Run the default beat tracker tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr) print('Estimated tempo: {:.2f} beats per minute'.format(tempo)) # 4. Convert the frame indices of beat events into timestamps D = np.abs(librosa.stft(y))