def applyOperation(operation, first, second): ft1, ffp = pluck(first['params'], 't1', 'fp') st1, sfp = pluck(second['params'], 't1', 'fp') if (ft1 != st1 or ffp != sfp): sg.Popup('Error', 't1 and fp must match') return None new_x = [x for x, _ in zip(first['x'], second['x'])] new_y = applyOperationSimple(operation, first['y'], second['y']) params = second['params'] isPeriodic = first['isPeriodic'] and second['isPeriodic'] if isPeriodic: fT = first['params']['T'] sT = second['params']['T'] params['T'] = fT if fT == sT else fT * sT return { 'name': second['name'], 'displayName': f"{first['displayName']}{operation}{second['displayName']}", 'isDiscrete': (first['isDiscrete'] or second['isDiscrete']), 'isComplex': True, 'isPeriodic': isPeriodic, 'x': new_x, 'y': new_y, "params": second['params'], "operation": operation, "firstOperand": first, "secondOperand": second }
def onComputeErrorParameters(window, values, storedSignals): selectedGraphs = values['selectedGraphs'] if len(selectedGraphs) != 1: sg.Popup('Error!', 'Select 1 graph for error parameters computation!') return None selectedSignal = storedSignals[getSelectedGraphIndex(selectedGraphs[0])] name, x, y, params = pluck(selectedSignal, 'name', 'x', 'y', 'params') actualValuesForSignal = computeSignal(selectedSignal, x) #test newSignal = { **selectedSignal, 'displayName': f"computed: {selectedSignal['displayName']}", 'y': actualValuesForSignal } addToSelectionList(window, newSignal, storedSignals) #test errors = calculateErrorStatistics(selectedSignal['y'], actualValuesForSignal) MSE, SNR, PSNR, MD, ENOB = pluck(errors, 'MSE', 'SNR', 'PSNR', 'MD', 'ENOB') sg.Popup( 'Signal properties', f""" MSE: {MSE} SNR: {SNR} PSNR: {PSNR} MD: {MD} ENOB: {ENOB} """)
def generate_signal(signalType, param_values): d, fp, t1 = pluck(param_values, 'd', 'fp', 't1') n = int(d * fp) param_values['n'] = n x_values = np.linspace(t1, t1+d, n + 1) y_values = signals[signalType]['fn'](x_values, param_values) return [x_values, y_values]
def computeSignal(signal, x_values): if signal['isComplex'] == True: operation, firstOperand, secondOperand = pluck(signal, 'operation', 'firstOperand', 'secondOperand') LsigVal, RsigVal = None, None if(firstOperand['isComplex']): LsigVal = computeSignal(firstOperand, x_values) else: sigName = firstOperand['name'] LsigVal = signals[sigName]['fn'](x_values, firstOperand['params']) if(secondOperand['isComplex']): RsigVal = computeSignal(secondOperand, x_values) else: sigName = secondOperand['name'] RsigVal = signals[sigName]['fn'](x_values, secondOperand['params']) return applyOperationSimple(operation, LsigVal, RsigVal) else: sigName = signal['name'] return signals[sigName]['fn'](x_values, signal['params'])
def uniform_noise(x_values, params): A, n = pluck(params, 'A', 'n') return list(map(lambda v: 2 * A * v - A, np.random.rand(n)))
def impulse_noise(x_values, params): A, p = pluck(params, 'A', 'p') return [A if np.random.rand() < p else 0 for x in range(len(x_values))]
def unit_impulse(x_values, params): A, ts = pluck(params, 'A', 'ts') return list(map(lambda v: A if v == ts else 0, x_values))
def unit_step(x_values, params): A, ts = pluck(params, 'A', 'ts') return list(map(lambda t: unit_step_helper(t, A, ts), x_values))
def sawtooth(x_values, params): A, t1, T, kw = pluck(params, 'A', 't1', 'T', 'kw') return list(map(lambda t: sawtooth_helper(t, t1, T, kw, A), x_values))
def rectangular_symmetrical(x_values, params): A, t1, T, kw = pluck(params, 'A', 't1', 'T', 'kw') return list( map(lambda t: -A if rect_helper(t, t1, T, kw, A) == -1 else A, x_values))
def sin_full_rectified(x_values, params): A, t1, T = pluck(params, 'A', 't1', 'T') return list(map(lambda t: A * abs(sin_helper(t, t1, T)), x_values))
def sin_half_rectified(x_values, params): A, t1, T = pluck(params, 'A', 't1', 'T') return list( map( lambda t: 0.5 * A * (sin_helper(t, t1, T) + abs(sin_helper(t, t1, T))), x_values))
def sin(x_values, params): A, t1, T = pluck(params, 'A', 't1', 'T') return list(map(lambda t: A * sin_helper(t, t1, T), x_values))
def gaussian_noise(x_values, params): A, n = pluck(params, 'A', 'n') return np.random.normal(0, 2 * A, n)
def onSimulate(window, values, storedSignals): sim_params = pluck(values, 'stu', 'ov', 'sv', 'sp', 'ssf', 'bl', 'rp', 'sim_len') stu, object_v, signal_v, signal_T, signal_sampling_freq, buffer_len, report_T, simulation_length = [ try_float(param) for param in sim_params ] buffer_len, report_T = int(buffer_len), int(report_T) lesser_signal_periods_per_signal_T = 0.05 lesser_signal_T = signal_T * lesser_signal_periods_per_signal_T # lesser_signal_T = lesser_signal_T * 1.1 lesser_signal_T = 0.00636942675 signal_T = 0.05263157894 SIGNAL_LENGTH = 100 param_values = { 'A': 1, 't1': 0, 'T': signal_T, 'd': SIGNAL_LENGTH, 'fp': signal_sampling_freq } lesser_param_values = {**param_values, 'T': lesser_signal_T} first = create_signal_with_metadata('sin', param_values) second = create_signal_with_metadata('sin', lesser_param_values) probing_signal = ops.applyOperation("+", first, second) probing_values = probing_signal['y'] time = .0 outgoing_buffer = [0] * buffer_len incoming_buffer = [0] * buffer_len object_real_distance = 100 samples_per_stu = signal_sampling_freq * stu i = buffer_len distances = [] r_distances = [] correlation_serieses = [] correlation = [] print('x') while time < simulation_length: first_sample_index = int((i - buffer_len) * samples_per_stu) outgoing_buffer = probing_values[ first_sample_index:first_sample_index + buffer_len] # jak nizej delay_samples = int( abs(object_real_distance) / signal_v * signal_sampling_freq * 2) incoming_buffer = probing_values[first_sample_index + delay_samples:first_sample_index + buffer_len + delay_samples] #jak wypelniac print( f"{first_sample_index} -- {probing_values[first_sample_index + buffer_len]}" ) print( f"{delay_samples} -- {probing_values[first_sample_index + delay_samples]}" ) if ((i - buffer_len) % report_T == 0 and i > buffer_len): correlation = cor.convCorrelation(outgoing_buffer, incoming_buffer) correlation_half = correlation[int(len(correlation) / 2):] if correlation_half.size == 0: break offset = np.argmax(correlation_half) distance = offset * signal_v / signal_sampling_freq / 2 distances.append(distance) r_distances.append(object_real_distance) print( f"distance: {object_real_distance} calculated: {distance} offset {offset}" ) if (i - buffer_len) % report_T * 5: correlation_serieses.append(correlation) object_real_distance -= object_v * stu time += stu i += 1 print(f"time: {time}") show([distances, r_distances, correlation])