def autocorrelation_addition(code, num): """ Given a code composed of num subcodes (of the same length), computes the autocorrelation of each and adds them, outputting the psl of that addition. :param code: initial sequence :type code: array :param num: number of sub_codes :type num: int :return: psl of the sum of the codes (how near-alternating the codes are). """ if len( code ) % num != 0: # throw an error if we cannot properly divide the code raise ValueError else: length = len(code) / num # length of each code current_count = 1 subcodes = [] for i in range(num): # compute the subcodes new_code = code[length * i:length * (i + 1)] subcodes.append(new_code) autocorrelation_lsts = [] for subcode in subcodes: # compute their autocorrelations autocorrelation_lsts.append(con_psl_matrix(subcode)) final_autocorrelation = [] for i in range(len(autocorrelation_lsts[0])): # add entry by entry sum = 0 for j in range(len(autocorrelation_lsts)): sum += autocorrelation_lsts[j][i] final_autocorrelation.append(sum) return second_max(final_autocorrelation)
def gen_alternating_codes(length): """ Function to generate pairs of alternating codes; uses the naive approach of looping over all codes. :param length: length of the codes in question :type length: int :return: none, just prints the codes. """ all_codes = gen_binary_codes(length) # generate all binary codes for code_1 in all_codes: # double for loop over all codes, sums their con_psl_matrixes and checks if they are alternating for code_2 in all_codes: x = sum_codes(con_psl_matrix(code_1), con_psl_matrix(code_2)) if is_alternating(x): print(x) print(code_1) print(code_2)
def fourth_alternating_code(): code = [-1, 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1] x_1 = np.linspace(0, math.pi, 200) y_1 = np.sin(x_1) sin_fns = [] for i in range(len(code)): x = np.linspace(2 * math.pi * i + math.pi / 2, 2 * math.pi * (i + 1) + math.pi / 2, 200) y = -np.sin(x) * code[i] sin_fns.append((x, y)) fig = plt.figure() ax = fig.add_subplot(111) fig.subplots_adjust(top=0.85) ax.add_patch( mpatches.Rectangle((math.pi / 2, -1), 2 * math.pi, 1, fill=True, color='g', alpha=0.5)) ax.add_patch( mpatches.Rectangle((math.pi / 2 + 2 * math.pi, 0), 4 * math.pi, 1, fill=True, color='g', alpha=0.5)) ax.add_patch( mpatches.Rectangle((math.pi / 2 + 6 * math.pi, -1), 4 * math.pi, 1, fill=True, color='g', alpha=0.5)) ax.add_patch( mpatches.Rectangle((math.pi / 2 + 10 * math.pi, 0), 2 * math.pi, 1, fill=True, color='g', alpha=0.5)) ax.add_patch( mpatches.Rectangle((math.pi / 2 + 12 * math.pi, -1), 2 * math.pi, 1, fill=True, color='g', alpha=0.5)) ax.add_patch( mpatches.Rectangle((math.pi / 2 + 14 * math.pi, 0), 2 * math.pi, 1, fill=True, color='g', alpha=0.5)) ax.add_patch( mpatches.Rectangle((math.pi / 2 + 16 * math.pi, -1), 4 * math.pi, 1, fill=True, color='g', alpha=0.5)) ax.add_patch( mpatches.Rectangle((math.pi / 2 + 20 * math.pi, 0), 2 * math.pi, 1, fill=True, color='g', alpha=0.5)) ax.add_patch( mpatches.Rectangle((math.pi / 2 + 22 * math.pi, -1), 4 * math.pi, 1, fill=True, color='g', alpha=0.5)) ax.add_patch( mpatches.Rectangle((math.pi / 2 + 2 * math.pi, -1), 0, 2, fill=True, color='#000000')) ax.add_patch( mpatches.Rectangle((math.pi / 2 + 6 * math.pi, -1), 0, 2, fill=True, color='#000000')) ax.add_patch( mpatches.Rectangle((math.pi / 2 + 10 * math.pi, -1), 0, 2, fill=True, color='#000000')) ax.add_patch( mpatches.Rectangle((math.pi / 2 + 12 * math.pi, -1), 0, 2, fill=True, color='#000000')) ax.add_patch( mpatches.Rectangle((math.pi / 2 + 14 * math.pi, -1), 0, 2, fill=True, color='#000000')) ax.add_patch( mpatches.Rectangle((math.pi / 2 + 16 * math.pi, -1), 0, 2, fill=True, color='#000000')) ax.add_patch( mpatches.Rectangle((math.pi / 2 + 20 * math.pi, -1), 0, 2, fill=True, color='#000000')) ax.add_patch( mpatches.Rectangle((math.pi / 2 + 22 * math.pi, -1), 0, 2, fill=True, color='#000000')) for i in range(len(sin_fns)): ax.plot(sin_fns[i][0], sin_fns[i][1], c='r') ax = fig.add_subplot(111) fig.subplots_adjust(top=0.85) ax.axis([0, 100, -5, 5]) ax.axis('off') plt.show() plt.clf() autocorrelation = con_psl_matrix(code) x = [] for i in range(len(autocorrelation)): x.append(i) fig = plt.figure() ax = fig.add_subplot(111) fig.subplots_adjust(top=0.85) ax.scatter(x, np.abs(autocorrelation)) ax.plot(x, np.abs(autocorrelation)) ax.axis([-1, 30, -1, 15]) ax.axis('off') plt.show() plt.clf()