def test(): chip = Chip("square_root") test_in = Stimulus(chip, "input", "float", [i * 0.1 for i in range(100)]) test_out = Response(chip, "output", "float") #create a filter component using the C code sqrt = Component("sqrt.c") #add an instance to the chip sqrt( chip, inputs={ "x": test_in, }, outputs={ "sqrt_x": test_out, }, ) #run the simulation chip.simulation_reset() while len(test_out) < 100: chip.simulation_step() pyplot.plot(list(test_in), list(test_out)) pyplot.xlim(0, 10.1) pyplot.title("Square Root of x") pyplot.xlabel("x") pyplot.ylabel("$\\sqrt{x}$") pyplot.savefig("../docs/source/examples/images/example_1.png") pyplot.show()
def test(): chip = Chip("square_root") test_in = Stimulus(chip, "input", "float", [i*0.1 for i in range(100)]) test_out = Response(chip, "output", "float") #create a filter component using the C code sqrt = Component("sqrt.c") #add an instance to the chip sqrt( chip, inputs = { "x":test_in, }, outputs = { "sqrt_x":test_out, }, ) #run the simulation chip.simulation_reset() while len(test_out) < 100: chip.simulation_step() pyplot.plot(list(test_in), list(test_out)) pyplot.xlim(0, 10.1) pyplot.title("Square Root of x") pyplot.xlabel("x") pyplot.ylabel("$\\sqrt{x}$") pyplot.savefig("../docs/source/examples/images/example_1.png") pyplot.show()
def test(): from math import pi from numpy import abs from scipy import fft from scipy.signal import firwin from matplotlib import pyplot from chips.api.api import Chip, Stimulus, Response, Wire, Component #create a chip chip = Chip("filter_example") #low pass filter half nyquist 50 tap kernel = Stimulus(chip, "kernel", "float", firwin(50, 0.5, window="blackman")) #impulse response input_ = Stimulus(chip, "input", "float", [1.0] + [0.0 for i in range(1024)]) output = Response(chip, "output", "float") #create a filter component using the C code fir_comp = Component("fir.c") #add an instance to the chip fir_inst_1 = fir_comp( chip, inputs={ "a": input_, "k": kernel, }, outputs={ "z": output, }, parameters={ "N": len(kernel) - 1, }, ) #run the simulation chip.simulation_reset() while len(output) < 1024: chip.simulation_step() #plot the result pyplot.semilogy(abs(fft(list(output)))[0:len(output) / 2]) pyplot.title("Magnitude of Impulse Response") pyplot.xlim(0, 512) pyplot.xlabel("X Sample") pyplot.savefig("../docs/source/examples/images/example_6.png") pyplot.show()
def test(): from math import pi from numpy import abs from scipy import fft from scipy.signal import firwin from matplotlib import pyplot from chips.api.api import Chip, Stimulus, Response, Wire, Component #create a chip chip = Chip("filter_example") #low pass filter half nyquist 50 tap kernel = Stimulus(chip, "kernel", "float", firwin(50, 0.5, window="blackman")) #impulse response input_ = Stimulus(chip, "input", "float", [1.0] + [0.0 for i in range(1024)]) output = Response(chip, "output", "float") #create a filter component using the C code fir_comp = Component("fir.c") #add an instance to the chip fir_inst_1 = fir_comp( chip, inputs = { "a":input_, "k":kernel, }, outputs = { "z":output, }, parameters = { "N":len(kernel)-1, }, ) #run the simulation chip.simulation_reset() while len(output) < 1024: chip.simulation_step() #plot the result pyplot.semilogy(abs(fft(list(output)))[0:len(output)/2]) pyplot.title("Magnitude of Impulse Response") pyplot.xlim(0, 512) pyplot.xlabel("X Sample") pyplot.savefig("../docs/source/examples/images/example_6.png") pyplot.show()
def test(): chip = Chip("taylor") stimulus = arange(-2*pi, 2.0*pi, pi/25) x = Stimulus(chip, "x", "double", stimulus) sin_x = Response(chip, "sin_x", "double") cos_x = Response(chip, "cos_x", "double") #create a filter component using the C code sqrt = Component("taylor.c") #add an instance to the chip sqrt( chip, inputs = { "x":x, }, outputs = { "sin_x":sin_x, "cos_x":cos_x, }, ) #run the simulation chip.simulation_reset() while len(cos_x) < len(x): chip.simulation_step() x = list(x) sin_x = list(sin_x)[:100] cos_x = list(cos_x)[:100] pyplot.xticks( [-2.0*pi, -pi, 0, pi, 2.0*pi], [r'$-2\pi$', r"$-\pi$", r'$0$', r'$\pi$', r'$2\pi$']) pyplot.plot(x, sin_x, label="sin(x)") pyplot.plot(x, cos_x, label="cos(x)") pyplot.ylim(-1.1, 1.1) pyplot.xlim(-2.2 * pi, 2.2 * pi) pyplot.title("Trig Functions") pyplot.xlabel("x (radians)") pyplot.legend(loc="upper left") pyplot.savefig("../docs/source/examples/images/example_2.png") pyplot.show()
def test(): chip = Chip("taylor") stimulus = arange(-2 * pi, 2.0 * pi, pi / 25) x = Stimulus(chip, "x", "double", stimulus) sin_x = Response(chip, "sin_x", "double") cos_x = Response(chip, "cos_x", "double") #create a filter component using the C code sqrt = Component("taylor.c") #add an instance to the chip sqrt( chip, inputs={ "x": x, }, outputs={ "sin_x": sin_x, "cos_x": cos_x, }, ) #run the simulation chip.simulation_reset() while len(cos_x) < len(x): chip.simulation_step() x = list(x) sin_x = list(sin_x)[:100] cos_x = list(cos_x)[:100] pyplot.xticks([-2.0 * pi, -pi, 0, pi, 2.0 * pi], [r'$-2\pi$', r"$-\pi$", r'$0$', r'$\pi$', r'$2\pi$']) pyplot.plot(x, sin_x, label="sin(x)") pyplot.plot(x, cos_x, label="cos(x)") pyplot.ylim(-1.1, 1.1) pyplot.xlim(-2.2 * pi, 2.2 * pi) pyplot.title("Trig Functions") pyplot.xlabel("x (radians)") pyplot.legend(loc="upper left") pyplot.savefig("../docs/source/examples/images/example_2.png") pyplot.show()
def test(): chip = Chip("fft") x_re = [0.0 for i in range(1024)] x_im = [0.0 for i in range(1024)] x_re[0:63] = [sin(2.0 * pi * (i/64.0)) for i in range(64)] x_re = Stimulus(chip, "x_re", "double", x_re) x_im = Stimulus(chip, "x_im", "double", x_im) fft_x_re = Response(chip, "fft_x_re", "double") fft_x_im = Response(chip, "fft_x_im", "double") #create a filter component using the C code fft = Component("fft.c") #add an instance to the chip fft( chip, inputs = { "x_re":x_re, "x_im":x_im, }, outputs = { "fft_x_re":fft_x_re, "fft_x_im":fft_x_im, }, ) #run the simulation chip.simulation_reset() while len(fft_x_im) < len(x_im): chip.simulation_step() x_re = list(x_re) x_im = list(x_im) fft_x_re = list(fft_x_re)[:len(x_re)] fft_x_im = list(fft_x_im)[:len(x_im)] time_complex = [i + (j*1.0) for i, j in zip(x_re, x_im)] numpy_complex = s.fft(time_complex) numpy_magnitude = n.abs(numpy_complex) chips_complex = [i + (j*1.0j) for i, j in zip(fft_x_re, fft_x_im)] chips_magnitude = n.abs(chips_complex) f, subplot = pyplot.subplots(3, sharex=True) pyplot.subplots_adjust(hspace=1.0) subplot[0].plot(x_re, 'g') subplot[1].plot(numpy_magnitude, 'r') subplot[2].plot(chips_magnitude, 'b') pyplot.xlim(0, 1023) subplot[0].set_title("Time Domain Signal (64 point sine)") subplot[1].set_title("Frequency Spectrum - Numpy") subplot[2].set_title("Frequency Spectrum - Chips") subplot[0].set_xlabel("Sample") subplot[1].set_xlabel("Sample") subplot[2].set_xlabel("Sample") pyplot.savefig("../docs/source/examples/images/example_5.png") pyplot.show()