בינה מאלכותית RB14-18 : זיהוי צלילים בעזרת בינה מלאכותית LSTM
let read file and show it on Raw waveform and its spectrogram
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# -*- coding: utf-8 -*- """ Read a WAV file and plot its waveform + spectrogram with file information and parameters """ import numpy as np import matplotlib.pyplot as plt from scipy.io import wavfile import os # ========================================================= # 1. Load WAV file # ========================================================= file_path = r"d:\temp\ring1.wav" fs, data = wavfile.read(file_path) # File info file_name = os.path.basename(file_path) num_samples = len(data) duration = num_samples / fs print("File path:", file_path) print("File name:", file_name) print("Sampling rate (Hz):", fs) print("Data shape:", data.shape) print("Duration (s):", duration) # If stereo, take only the first channel channels = 1 if data.ndim > 1: channels = data.shape[1] data = data[:,0] # ========================================================= # 2. Normalize to [-1, 1] if PCM integers # ========================================================= if data.dtype != np.float32 and data.dtype != np.float64: data = data / np.max(np.abs(data)) # ========================================================= # 3. Plot waveform # ========================================================= plt.figure(figsize=(12,4)) time_axis = np.arange(len(data)) / fs plt.plot(time_axis, data) plt.title(f"Waveform of {file_name}\nPath: {file_path}\nDuration: {duration:.2f}s, Fs={fs}Hz, Channels={channels}") plt.xlabel("Time (s)") plt.ylabel("Amplitude") plt.grid(True) plt.show() # ========================================================= # 4. Plot spectrogram # ========================================================= plt.figure(figsize=(12,6)) Pxx, freqs, bins, im = plt.specgram(data, NFFT=1024, # FFT window size Fs=fs, # sampling frequency noverlap=512, # overlap between windows cmap="viridis") plt.title(f"Spectrogram of {file_name}\nDuration: {duration:.2f}s, Fs={fs}Hz, Channels={channels}") plt.xlabel("Time (s)") plt.ylabel("Frequency (Hz)") plt.colorbar(label="Intensity (dB)") plt.show() |