Kompyuter Injineringi fakulteti (kompyuter injineringi) yonalishi 4-bosqich 021-20 - guruh talabasi Nazmiddin Sirojiddinov Rustem O’glining Tizim va Signallarni Qayta ishlash
fanidan 3-Amaliy ish vazifasi
Tizim va Signallarni Qayta ishlash
Bajardi : Nazmiddin Sirojiddinov Rustem O’gli 4-kurs (021-20)
34-variant
import numpy as np
import matplotlib.pyplot as plt
from scipy.linalg import hadamard
from scipy.fft import fft
# Given signal values
signal = np.array([11,4,10,2,0,13,14,14,7,9,11,5,8])
# Frame size
frame_size = 2
# Pad signal with zeros to make its length a multiple of frame_size
if len(signal) % frame_size != 0:
signal = np.pad(signal, (0, frame_size - len(signal) % frame_size))
# Reshape signal into frames
frames = signal.reshape(-1, frame_size)
# Create Hadamard matrix
H = hadamard(frame_size)
# Calculate spectrum using Hadamard transform for each frame
spectrum_hadamard = np.dot(frames, H)
# Calculate spectrum using FFT for each frame
spectrum_fft = fft(frames)
# Plot spectrum values
plt.figure(figsize=(12, 6))
plt.subplot(121)
plt.plot(spectrum_hadamard.flatten())
plt.title('Spectrum values (Hadamard)')
plt.subplot(122)
plt.plot(spectrum_fft.flatten().real) # plot only the real part
plt.title('Spectrum values (FFT)')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from scipy.fft import fft
signal = np.array([11,4,10,2,0,13,14,14,7,9,11,5,8])
frame_size = 2
def create_hadamard_matrix(n):
hadamard_matrix = np.array([[1]])
while hadamard_matrix.shape[0] < n:
hadamard_matrix = np.block([[hadamard_matrix, hadamard_matrix], [hadamard_matrix, -hadamard_matrix]])
return hadamard_matrix
hadamard_matrix = create_hadamard_matrix(frame_size)
num_frames = int(np.ceil(len(signal) / frame_size))
padded_values = np.pad(signal, (0, num_frames * frame_size - len(signal)), 'constant')
frames = padded_values.reshape((num_frames, frame_size))
hadamard_spectra = []
for frame in frames:
hadamard_spectrum = np.dot(hadamard_matrix, frame)
hadamard_spectra.append(hadamard_spectrum)
hadamard_combined_spectrum = np.concatenate(hadamard_spectra)
fft_spectra = []
for frame in frames:
fft_spectrum = fft(frame)
fft_spectra.append(fft_spectrum)
fft_combined_spectrum = np.concatenate(fft_spectra)
plt.figure(figsize=(12, 8))
plt.subplot(3, 1, 1)
plt.stem(hadamard_combined_spectrum, use_line_collection=True, label='Adamar')
plt.title('Adamar ozgartishning spektri')
plt.legend()
plt.subplot(3, 1, 2)
plt.stem(np.abs(fft_combined_spectrum), use_line_collection=True, label='TFo')
plt.title('TFo ning spektri')
plt.legend()
def inverse_hadamard_transform(vector):
n = len(vector)
hadamard_matrix = create_hadamard_matrix(n)
inverse_hadamard = hadamard_matrix.T / n
return np.dot(inverse_hadamard, vector)
inverse_hadamard_spectra = []
for frame in hadamard_spectra:
inverse_hadamard_spectrum = inverse_hadamard_transform(frame)
inverse_hadamard_spectra.append(inverse_hadamard_spectrum)
inverse_hadamard_combined_spectrum = np.concatenate(inverse_hadamard_spectra)
plt.subplot(3, 1, 3)
plt.stem(inverse_hadamard_combined_spectrum, use_line_collection=True, label='Qayta tiklangan signal')
plt.title('Amplitudani chastotadan vaqtga otkazish')
plt.legend()
plt.tight_layout()
plt.show()
Dastur google colabdagi screenshotlari
|