Spectra output from conversion of matlab to python

35 Views Asked by At

enter image description hereI have converted a set of matlab files to python code for a set of spectra. Im trying to get one plot or a combined spectra. Im getting some strange output that I am hoping someone can help me fix.

Spectra

This is the code I have tried.


import os
import matplotlib.pyplot as plt
from scipy.io import loadmat
%matplotlib inline
%matplotlib widget

# Specify the directory where your .mat files are located
mat_files_directory = '/Users/XXXX/Desktop/Tau Ceti/'

# List all .mat files in the directory
mat_files = [file for file in os.listdir(mat_files_directory) if file.endswith('wcal.mat')]

# Prepare lists to store data for all files
all_line_frequencies = []
all_line_amplitudes = []

# Loop over each .mat file
for mat_file in mat_files:
    mat_data = loadmat(os.path.join(mat_files_directory, mat_file))

    spectrum_amplitude = mat_data['fib4_spec']
    spectrum_wave = mat_data['fib4_spec_wave']
    num_lines = spectrum_amplitude.shape[0]

    # Store data for this file
    all_line_frequencies.append(spectrum_wave)
    all_line_amplitudes.append(spectrum_amplitude)

# Create a single plot
plt.figure(figsize=(14, 8))

for file_index, (frequencies, amplitudes) in enumerate(zip(all_line_frequencies, all_line_amplitudes)):
    num_lines = amplitudes.shape[0]  # Define num_lines here for each spectrum
    for line_index in range(num_lines):
        line_amplitude = amplitudes[line_index]
        line_frequency = frequencies[line_index]
        plt.plot(line_frequency, line_amplitude, label=f'File {file_index+1}, Line {line_index+1}')

plt.xlabel('Frequency')
plt.ylabel('Amplitude')
plt.title('Combined Spectrum Comparison')
plt.show()
enter image description here
0

There are 0 best solutions below