#HMMicToMatPlot0202-2025.py
import pyaudio
import numpy as np
import matplotlib.pyplot as plt
# Audio stream configuration
CHUNK = 1024 # Buffer size
FORMAT = pyaudio.paInt16 # 16-bit audio format
CHANNELS = 1 # Mono audio
RATE = 44100 # Sampling rate (Hz)
def live_plot_mic():
p = pyaudio.PyAudio()
# Open microphone stream
mic_stream = p.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
# Create a Matplotlib figure for real-time plotting
plt.ion() # Enable interactive mode
fig, ax = plt.subplots()
x = np.arange(0, CHUNK) # X-axis (samples)
y = np.zeros(CHUNK) # Y-axis (initial blank signal)
line, = ax.plot(x, y)
ax.set_ylim(-32768, 32768) # 16-bit audio range
ax.set_xlim(0, CHUNK) # Number of samples
print("Streaming microphone input to live plot... Press Ctrl+C to stop.")
try:
while True:
data = mic_stream.read(CHUNK, exception_on_overflow=False) # Read audio data
audio_wave = np.frombuffer(data, dtype=np.int16) # Convert to NumPy array
line.set_ydata(audio_wave) # Update Y-data
plt.draw()
plt.pause(0.01) # Small pause to allow GUI update
except KeyboardInterrupt:
print("Stopping microphone streaming.")
finally:
mic_stream.stop_stream()
mic_stream.close()
p.terminate()
plt.ioff()
plt.show() # Final static plot
if __name__ == "__main__":
live_plot_mic()
../
amazon
HiLetgo DDS AD9850 Signal Generator Module 0-40MHz Sine Wave and 2 Square Wave Output IC Test Equipment
2PCS AD9850 DDS Signal Generator Module 0-40MHz 2 Sine Wave and 2 Square Wave Output
../
Raspberry Pi: Pi HAT Waveform Generator for Raspberry Pi 3B+
AD9850 DDS Signal Generator Module
https://www.youtube.com/watch?v=PlSp35tS7GE
../
Introduction to sound synthesis with Python: The Basics of Digital Audio Programming
https://www.youtube.com/watch?v=2jYpAamxb-8
../
Forget All Other Antennas. Watch All the Channels in the World with One Antenna!
https://www.youtube.com/watch?v=5YMkG9dw-kM
https://www.youtube.com/watch?v=5YMkG9dw-kM
audio synthesizer signal generator python example
Explanation:
Import necessary libraries:
numpy for generating the signal waveform
sounddevice for playing the audio
generate_sine_wave function:
Creates a sine wave signal using np.sin.
Takes frequency, duration, and amplitude as arguments.
Returns the generated signal as a numpy array.
generate_square_wave function:
Creates a square wave signal using np.sign.
Takes the same arguments as generate_sine_wave.
Returns the generated signal as a numpy array.
play_sound function:
Plays the audio signal using sd.play.
Waits for the playback to finish using sd.wait.
Main execution block:
Generates a sine wave with a frequency of 440 Hz (A4) and a duration of 2 seconds.
Plays the generated sine wave.
Generates a square wave with a frequency of 220 Hz (A3) and a duration of 1 second.
Plays the generated square wave.
import numpy as np
import sounddevice as sd
def generate_sine_wave(frequency, duration, amplitude=0.5, sample_rate=44100):
"""Generates a sine wave signal."""
t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
signal = amplitude * np.sin(2 * np.pi * frequency * t)
return signal
def generate_square_wave(frequency, duration, amplitude=0.5, sample_rate=44100):
"""Generates a square wave signal."""
t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
signal = amplitude * np.sign(np.sin(2 * np.pi * frequency * t))
return signal
def play_sound(signal, sample_rate=44100):
"""Plays the generated signal."""
sd.play(signal, sample_rate)
sd.wait()
if __name__ == "__main__":
# Generate and play a sine wave
sine_wave = generate_sine_wave(440, 2) # 440 Hz, 2 seconds
play_sound(sine_wave)
# Generate and play a square wave
square_wave = generate_square_wave(220, 1) # 220 Hz, 1 second
play_sound(square_wave)