# Gavin S Wallace, University of Strathclyde, LIGO Hanford

# A script to print the filter required for damping/exciting resonant modes for a given frequency and phase. Based from Patrick J Thomas (California Institute of Technology, LIGO Hanford) script for damping violin modes

# Phase input option creates:
# bandpass: A bandpass filter centered at the selected frequency
# -30: A phase shift of -30 degrees and unity gain at the selected frequency
# +30: A phase shift of +30 degrees and unity gain at the selected frequency
# -60: A phase shift of -60 degrees and unity gain at the selected frequency
# +60: A phase shift of +60 degrees and unity gain at the selected frequency
# +180: A phase shift of +180 degrees and unity gain at the selected frequency
# gain: A gain of 100 dB at all frequencies


def print_design_filter(frequency, phase):

	if phase == 'bandpass':
		print('butter("BandPass",4,{0},{1})gain(120,"dB")'.format(max(0, frequency - .05), min(8192, frequency + .05)))

	frequency = float(frequency)

	# phase

	Q = 50

	if phase == '-30':
		f0 = frequency / (1 + (0.58 / (2 * Q)))
		a = f0 / (2 * Q)
		b = f0

		print('zpk([0], [{0}+i*{1};{0}-i*{1}],1,"n")gain(2.28184e-05)'.format(a,b))

	elif phase == '+30':
		f0 = frequency / (1 - (0.58 / (2 * Q)))
		a = f0 / (2 * Q)
		b = f0

		print('zpk([0], [{0}+i*{1};{0}-i*{1}],1,"n")gain(2.28184e-05)'.format(a,b))

	elif phase == '-60':
		f0 = frequency / (1 + (1.71 / (2 * Q)))
		a = f0 / (2 * Q)
		b = f0

		print('zpk([0], [{0}+i*{1};{0}-i*{1}],1,"n")gain(4.06536e-05)'.format(a,b))

	elif phase == '+60':
		f0 = frequency / (1 - (1.71 / (2 * Q)))
		a = f0 / (2 * Q)
		b = f0

		print('zpk([0], [{0}+i*{1};{0}-i*{1}],1,"n")gain(4.06536e-05)'.format(a,b))

	elif phase == '+180':
		filter_file[module_name][5].design = 'gain(-1)'

	elif phase == 'gain':
		filter_file[module_name][9].design = 'gain(100000)'

freq = input("Frequency for filter: ")
phase = input("Phase of filter (-/+ 30, -/+ 60, + 180, gain): ")	

print_design_filter(freq, phase)

