Procházet zdrojové kódy

Исправлена ошибка в типе переменной MaxWidth. Добавлен скрипт на python, для теста cic фильтров.

ChStepan před 5 měsíci
rodič
revize
614dc75ec7

+ 1 - 1
src/src/Sim/DecimFilterWrapperTb.v

@@ -12,7 +12,7 @@ parameter	InDataWidth	=	14;
 
 parameter	K = N*20*($log10(M));
 parameter	BitGworth = $ceil(2**(K/20));
-parameter	MaxWidth = InDataWidth+BitGworth;
+parameter	integer MaxWidth = InDataWidth+BitGworth;
 
 parameter	[31:0]	Nco1PhaseInc	=	32'h40000000;
 parameter	[31:0]	Nco2PhaseInc	=	32'h0F5C28F6;

+ 87 - 0
src/src/Sim/FreqDomainCicFilt.py

@@ -0,0 +1,87 @@
+
+import os
+import sys
+import numpy as np
+import matplotlib.pyplot as plt
+
+
+
+# Open the input file and read the impulse response data
+def read_impulse_response(file_path):
+    if not os.path.exists(file_path):
+        print(f"Error: File {file_path} does not exist.")
+        sys.exit(1)
+
+    with open(file_path, 'r') as file:
+        lines = file.readlines()
+
+    impulse_response = []
+    for line in lines:
+        try:
+            value = float(line.strip())
+            impulse_response.append(value)
+        except ValueError:
+            print(f"Warning: Skipping invalid line '{line.strip()}' in {file_path}")
+
+    return np.array(impulse_response)
+
+def convert_to_frequency_domain(impulse_response):
+    # Perform FFT
+    frequency_response = np.fft.fft(impulse_response)
+    
+    frequency_response /= np.max(np.abs(frequency_response))
+    
+    return frequency_response
+
+# Plot the frequency response
+def plot_frequency_response(frequency_response, input_signal_time_domain, fs=50e6):
+    normalized_freq = np.fft.fftfreq(len(input_signal_time_domain), 1/fs) / fs
+    
+    plt.figure(figsize=(10, 6))
+    half_len = len(normalized_freq) // 2
+    plt.plot(normalized_freq[:half_len], np.abs(frequency_response[:half_len]), 
+             label='Signal Frequency Response')
+    plt.title('Frequency Response')
+    plt.xlabel('Normalized Frequency (f/fs)')
+    # Magnitude in dB
+    plt.plot(normalized_freq[:half_len], 20 * np.log10(np.abs(frequency_response[:half_len])), label='Magnitude (dB)')
+    plt.ylabel('Magnitude (dB)')
+    plt.ylim(-150, 10) 
+    plt.grid(True)
+    plt.legend()
+    plt.xlim(0, 0.5)
+    plt.show()
+
+# Main function to execute the conversion
+def main():
+    impulse_response_file = 'C:\S5243_FFT_REPO\src\src\Sim\FilteredData.txt'
+
+    filt_data_impulse_response = read_impulse_response(impulse_response_file)
+    filt_data_frequency_response = convert_to_frequency_domain(filt_data_impulse_response)
+    plot_frequency_response(filt_data_frequency_response,filt_data_impulse_response)
+    print("Frequency response conversion completed successfully.")
+    # Show the input signal 
+    input_signal_time_domain = read_impulse_response('C:\S5243_FFT_REPO\src\src\Sim\InputSignal.txt')
+    input_signal_frequency_response = convert_to_frequency_domain(input_signal_time_domain)
+    fs = 50e6  
+    freq_axis = np.fft.fftfreq(len(input_signal_time_domain), 1/fs) / 1e6
+    
+    plt.figure(figsize=(10, 6))
+    half_len = len(freq_axis) // 2
+    plt.plot(freq_axis[:half_len], np.abs(input_signal_frequency_response[:half_len]), label='Input Signal Frequency Response')
+    plt.title('Input Signal Frequency Response')
+    plt.xlabel('Frequency (MHz)')
+    plt.ylabel('Magnitude')
+    plt.grid()
+    plt.legend()
+    plt.xlim(0, fs/(2*1e6)) 
+    plt.show()
+
+    cic_filter_impulse_response = read_impulse_response('C:\S5243_FFT_REPO\src\src\Sim\ImpResp.txt')
+    cic_filter_frequency_response = convert_to_frequency_domain(cic_filter_impulse_response)
+    plot_frequency_response(cic_filter_frequency_response, cic_filter_impulse_response)
+    print("CIC filter frequency response conversion completed successfully.")
+    
+if __name__ == "__main__":
+    main()
+