Plotting Various Sounds on Graphs using Python and Matplotlib
×


Plotting Various Sounds on Graphs using Python and Matplotlib

868

Loading and Plotting a WAV File

import matplotlib.pyplot as plt
import numpy as np
import wave
import sys

def visualize(path: str):
    raw = wave.open(path)
    signal = raw.readframes(-1)
    signal = np.frombuffer(signal, dtype="int16")
    f_rate = raw.getframerate()
    time = np.linspace(0, len(signal) / f_rate, num=len(signal))
    plt.figure(figsize=(10, 4))
    plt.title("Sound Wave")
    plt.xlabel("Time (seconds)")
    plt.ylabel("Amplitude")
    plt.plot(time, signal)
    plt.show()

if __name__ == "__main__":
    path = sys.argv[1]
    visualize(path)

Generating Synthetic Sine Wave

import numpy as np
import matplotlib.pyplot as plt

sample_rate = 44100  # Samples per second
duration = 5.0       # Duration in seconds
frequency = 440.0    # Frequency in Hz

t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
amplitude = np.iinfo(np.int16).max
y = amplitude * np.sin(2 * np.pi * frequency * t)

plt.figure(figsize=(10, 4))
plt.plot(t, y)
plt.title("Sine Wave (440 Hz)")
plt.xlabel("Time (seconds)")
plt.ylabel("Amplitude")
plt.show()

Generating Other Waveforms

# Square Wave
y_square = amplitude * np.sign(np.sin(2 * np.pi * frequency * t))

plt.figure(figsize=(10, 4))
plt.plot(t, y_square)
plt.title("Square Wave (440 Hz)")
plt.xlabel("Time (seconds)")
plt.ylabel("Amplitude")
plt.show()

# Sawtooth Wave
y_sawtooth = amplitude * (2 * (t * frequency - np.floor(t * frequency + 0.5)))

plt.figure(figsize=(10, 4))
plt.plot(t, y_sawtooth)
plt.title("Sawtooth Wave (440 Hz)")
plt.xlabel("Time (seconds)")
plt.ylabel("Amplitude")
plt.show()

# Noise Signal
y_noise = amplitude * np.random.uniform(-1, 1, size=t.shape)

plt.figure(figsize=(10, 4))
plt.plot(t, y_noise)
plt.title("Noise Signal")
plt.xlabel("Time (seconds)")
plt.ylabel("Amplitude")
plt.show()

Conclusion

Visualizing sound waves is essential for audio analysis and signal processing. Using Python libraries like Matplotlib and NumPy, you can easily load audio files, generate synthetic waveforms, and plot them for deeper insight into sound data. This foundational approach enables further exploration in audio processing, machine learning, and related fields.


If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!



Best WordPress Hosting


Share:


Discount Coupons

Unlimited Video Generation

Best Platform to generate videos

Search and buy from Namecheap

Secure Domain for a Minimum Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat