Compare commits
No commits in common. "9e3ee37ecf9d2b86c738900265e6e5722e18d5c9" and "61c0615e0525f33ad1808702a9d16e77b7c3ad3b" have entirely different histories.
9e3ee37ecf
...
61c0615e05
@ -1,103 +0,0 @@
|
|||||||
import sympy as sm
|
|
||||||
import numpy as np
|
|
||||||
sm.init_printing()
|
|
||||||
|
|
||||||
s = sm.symbols('s')
|
|
||||||
z = sm.symbols('z')
|
|
||||||
T = sm.symbols('T')
|
|
||||||
|
|
||||||
#########################################################
|
|
||||||
print('PROBLEM 2:')
|
|
||||||
print('Part a:')
|
|
||||||
|
|
||||||
ZOH = (1-sm.exp(-s*T))/(s*T)
|
|
||||||
|
|
||||||
G_1_s = 1/s*ZOH
|
|
||||||
|
|
||||||
bilinear_s = 2/T *(z-1)/(z+1)
|
|
||||||
|
|
||||||
G_1_k = G_1_s.subs({s:bilinear_s}).simplify()
|
|
||||||
|
|
||||||
print("G_1_k = ")
|
|
||||||
sm.pprint(G_1_k)
|
|
||||||
|
|
||||||
print('Part b:')
|
|
||||||
|
|
||||||
theta_s_r_s = ZOH*1/s**2
|
|
||||||
theta_k_r_k = theta_s_r_s.subs({s:bilinear_s}).simplify()
|
|
||||||
print("theta_k_r_k = ")
|
|
||||||
sm.pprint(theta_k_r_k)
|
|
||||||
|
|
||||||
|
|
||||||
#########################################################
|
|
||||||
print('PROBLEM 3:')
|
|
||||||
A = sm.Matrix([[0, 1, 0, 0],
|
|
||||||
[0, 0, 1, 0],
|
|
||||||
[0, 0, 0, 1],
|
|
||||||
[1, 0, 0, 0]])
|
|
||||||
B = sm.Matrix([0, 0, 0, 1])
|
|
||||||
C = sm.Matrix([1, 0, 0, 0]).transpose()
|
|
||||||
D = sm.Matrix([1])
|
|
||||||
|
|
||||||
print('System Matricies A, B, C, D')
|
|
||||||
sm.pprint(A)
|
|
||||||
sm.pprint(B)
|
|
||||||
sm.pprint(C)
|
|
||||||
sm.pprint(D)
|
|
||||||
|
|
||||||
#Recursive Solution
|
|
||||||
k = sm.symbols('k', integer = True, real = True, positive = True)
|
|
||||||
|
|
||||||
def y(k,u):
|
|
||||||
term_1 = C * A**k * x_0
|
|
||||||
|
|
||||||
term_2 = sm.Matrix([0,0,0,0])
|
|
||||||
for j in range(np.size(u)):
|
|
||||||
term_2 = term_2 + A**(k-j-1) * B * u[j]
|
|
||||||
term_2 = C*term_2
|
|
||||||
|
|
||||||
term_3 = D*u[-1]
|
|
||||||
|
|
||||||
return term_1+term_2+term_3
|
|
||||||
|
|
||||||
print('Part c:')
|
|
||||||
x_0 = sm.Matrix([2, 1, 3, 0])
|
|
||||||
u = sm.Matrix([0])
|
|
||||||
|
|
||||||
output = y(k, u)
|
|
||||||
output = output[0].expand().simplify()
|
|
||||||
print('y(k) = ')
|
|
||||||
sm.pprint(output)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
print('Part d:')
|
|
||||||
x_0 = sm.Matrix([0, 0, 0, 0])
|
|
||||||
u = sm.Matrix([2, 1, 3, 0])
|
|
||||||
output = y(k, u)
|
|
||||||
output = output[0].expand().simplify()
|
|
||||||
print('y(k) = ')
|
|
||||||
sm.pprint(output)
|
|
||||||
|
|
||||||
print('Part e:')
|
|
||||||
print('These are the same exact response between parts C and D. This makes sense, becasue we defined our states as just being delays in a chain. The result is that the input at timestep k trickles down through each state in k+1, k+2, and k+3. This means that our states save our input in a way, s.t. loading this initial state mathematically produces an identical result as loading those inputs in one time step at a time. \n \n This is reflected by the two algebraeic expressions being the same.')
|
|
||||||
|
|
||||||
#########################################################
|
|
||||||
print('PROBLEM 4:')
|
|
||||||
print('Part a:')
|
|
||||||
|
|
||||||
"""
|
|
||||||
x(k+2) - x(k+1) + 0.25 x(k) = u(k+2)
|
|
||||||
|
|
||||||
Applying Z transform:
|
|
||||||
z^2 X - z X + 0.25 X = z^2 U
|
|
||||||
X (z^2 - z + 0.25) = z^2 U
|
|
||||||
X/U = z^2 / (z^2 - z + 0.25)
|
|
||||||
"""
|
|
||||||
# Use SymPy to do partial frac
|
|
||||||
z = sm.symbols('z')
|
|
||||||
|
|
||||||
X_U = z**2/(z**2 - z + 0.25)
|
|
||||||
sm.pprint(X_U.apart())
|
|
||||||
|
|
||||||
|
|
||||||
@ -1 +0,0 @@
|
|||||||
,danesabo,danesabo-laptop,24.02.2025 15:04,file:///home/danesabo/snap/libreoffice/336/.config/libreoffice/4;
|
|
||||||
|
Before Width: | Height: | Size: 29 KiB |
|
Before Width: | Height: | Size: 33 KiB |
|
Before Width: | Height: | Size: 29 KiB |
|
Before Width: | Height: | Size: 25 KiB |
|
Before Width: | Height: | Size: 30 KiB |
|
Before Width: | Height: | Size: 27 KiB |
|
Before Width: | Height: | Size: 30 KiB |
|
Before Width: | Height: | Size: 70 KiB |
|
Before Width: | Height: | Size: 29 KiB |
@ -1,101 +0,0 @@
|
|||||||
import glob
|
|
||||||
import os
|
|
||||||
import numpy as np
|
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
|
|
||||||
def load_spectrum_data(filename):
|
|
||||||
"""
|
|
||||||
Load spectrum data from a text file in the provided format.
|
|
||||||
|
|
||||||
This function:
|
|
||||||
- Reads the file line by line.
|
|
||||||
- Finds the "$DATA:" section.
|
|
||||||
- Skips the first line after "$DATA:" (assumed to be the channel range header).
|
|
||||||
- Collects all subsequent lines (until the next section indicated by a line starting with '$').
|
|
||||||
- Parses these lines into a NumPy array.
|
|
||||||
|
|
||||||
Parameters:
|
|
||||||
filename (str): Path to the spectrum file.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
numpy.ndarray: Array of count values.
|
|
||||||
"""
|
|
||||||
with open(filename, 'r') as f:
|
|
||||||
lines = f.readlines()
|
|
||||||
|
|
||||||
data_lines = []
|
|
||||||
in_data_section = False
|
|
||||||
skip_first_data_line = True # flag to skip the channel-range header
|
|
||||||
|
|
||||||
for line in lines:
|
|
||||||
# Look for the beginning of the data section
|
|
||||||
if line.strip().startswith("$DATA:"):
|
|
||||||
in_data_section = True
|
|
||||||
continue
|
|
||||||
if in_data_section:
|
|
||||||
# If we hit a new section header, exit the data section
|
|
||||||
if line.strip().startswith("$"):
|
|
||||||
break
|
|
||||||
# Skip the first line after "$DATA:" if it contains exactly two numbers (channel range header)
|
|
||||||
if skip_first_data_line:
|
|
||||||
parts = line.strip().split()
|
|
||||||
if len(parts) == 2:
|
|
||||||
skip_first_data_line = False
|
|
||||||
continue
|
|
||||||
skip_first_data_line = False # even if not two numbers, do not skip subsequent lines
|
|
||||||
# Append non-empty lines
|
|
||||||
if line.strip():
|
|
||||||
data_lines.append(line.strip())
|
|
||||||
|
|
||||||
# Combine the collected lines into one string and parse numbers
|
|
||||||
data_str = " ".join(data_lines)
|
|
||||||
# Convert the string of numbers into a NumPy array
|
|
||||||
data = np.fromstring(data_str, sep=' ')
|
|
||||||
return data
|
|
||||||
|
|
||||||
# Get a list of all .Spe files in the current directory
|
|
||||||
spe_files = glob.glob("*.Spe")
|
|
||||||
|
|
||||||
# To store data for the combined plot
|
|
||||||
combined_data = []
|
|
||||||
|
|
||||||
for file in spe_files:
|
|
||||||
# Load the spectrum data
|
|
||||||
spectrum_data = load_spectrum_data(file)
|
|
||||||
# Create an array for channel numbers (one per data point)
|
|
||||||
channels = np.arange(len(spectrum_data))
|
|
||||||
|
|
||||||
# Store the data for the combined plot (use file name without extension for legend)
|
|
||||||
base_name = os.path.splitext(os.path.basename(file))[0]
|
|
||||||
combined_data.append((base_name, channels, spectrum_data))
|
|
||||||
|
|
||||||
# Plot individual spectrum
|
|
||||||
plt.figure(figsize=(10, 6))
|
|
||||||
plt.step(channels, spectrum_data, where='mid', color='blue')
|
|
||||||
plt.xlabel('Channel')
|
|
||||||
plt.ylabel('Counts')
|
|
||||||
plt.title(f"Spectrum: {base_name}")
|
|
||||||
plt.grid(True)
|
|
||||||
plt.tight_layout()
|
|
||||||
|
|
||||||
# Save individual figure as PNG
|
|
||||||
output_filename = base_name + '.png'
|
|
||||||
plt.savefig(output_filename)
|
|
||||||
plt.close() # Close the figure to free memory
|
|
||||||
|
|
||||||
# Now create a combined plot with all spectra
|
|
||||||
plt.figure(figsize=(12, 8))
|
|
||||||
for name, channels, spectrum_data in combined_data:
|
|
||||||
# Do not specify a color so that the default cycle gives different colors for each curve
|
|
||||||
plt.step(channels, spectrum_data, where='mid', label=name)
|
|
||||||
|
|
||||||
plt.xlabel('Channel')
|
|
||||||
plt.ylabel('Counts')
|
|
||||||
plt.title("Combined Spectrum Data")
|
|
||||||
plt.grid(True)
|
|
||||||
plt.legend()
|
|
||||||
plt.tight_layout()
|
|
||||||
|
|
||||||
# Save and display the combined plot
|
|
||||||
plt.savefig('combined_spectrum.png')
|
|
||||||
plt.show()
|
|
||||||