From fa54a994523a589e29d0b09094f4b2c208771fdc Mon Sep 17 00:00:00 2001
From: Pratham Vadhulas <71392285+rp-bot@users.noreply.github.com>
Date: Tue, 17 Sep 2024 14:59:25 -0400
Subject: [PATCH] Created using Colab
---
.../Convolution_Reverb.ipynb | 218 ++++++++++++++++++
1 file changed, 218 insertions(+)
create mode 100644 Chapter2_(WaveShaping)/Convolution_Reverb.ipynb
diff --git a/Chapter2_(WaveShaping)/Convolution_Reverb.ipynb b/Chapter2_(WaveShaping)/Convolution_Reverb.ipynb
new file mode 100644
index 0000000..7dfc65c
--- /dev/null
+++ b/Chapter2_(WaveShaping)/Convolution_Reverb.ipynb
@@ -0,0 +1,218 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "authorship_tag": "ABX9TyNFLYWu+TwqUMC2sKgDOQeX",
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ ""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "NbHwszlSMyau",
+ "outputId": "15611311-71b5-43a3-f192-57c2773560c0"
+ },
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Downloading...\n",
+ "From: https://drive.google.com/uc?id=1SX4O2LDen4334ED5haHvlOqH3OpNEXFz\n",
+ "To: /content/gun_shot_sfx.wav\n",
+ "\r 0% 0.00/799k [00:00, ?B/s]\r100% 799k/799k [00:00<00:00, 115MB/s]\n"
+ ]
+ }
+ ],
+ "source": [
+ "!gdown 1SX4O2LDen4334ED5haHvlOqH3OpNEXFz"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "import numpy as np\n",
+ "import matplotlib.pyplot as plt\n",
+ "from scipy.io import wavfile\n",
+ "from scipy.signal import convolve"
+ ],
+ "metadata": {
+ "id": "Sbc6vaZqP9V7"
+ },
+ "execution_count": 8,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Load the gunshot sound effect wave file\n",
+ "sample_rate, gunshot_wave = wavfile.read('gun_shot_sfx.wav')\n",
+ "\n",
+ "# Ensure the gunshot_wave is a 1D array (mono)\n",
+ "if gunshot_wave.ndim > 1:\n",
+ " gunshot_wave = gunshot_wave.mean(axis=1)"
+ ],
+ "metadata": {
+ "id": "ASaGLZRRRsWC"
+ },
+ "execution_count": 10,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Generate a sine wave\n",
+ "duration = 1.0 # seconds\n",
+ "frequency = 440.0 # Hz (A4 note)\n",
+ "t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)\n",
+ "sine_wave = 0.5 * np.sin(2 * np.pi * frequency * t)"
+ ],
+ "metadata": {
+ "id": "IkjFvlE9TqDr"
+ },
+ "execution_count": 11,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Perform convolution\n",
+ "convolved_wave = convolve(sine_wave, gunshot_wave, mode='same')"
+ ],
+ "metadata": {
+ "id": "yfYYI4tzTfXy"
+ },
+ "execution_count": 12,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Normalize the convolved wave to prevent clipping\n",
+ "convolved_wave = convolved_wave / np.max(np.abs(convolved_wave))\n",
+ "\n",
+ "# Save the convolved result to a new WAV file\n",
+ "wavfile.write('convolved_output.wav', sample_rate, convolved_wave.astype(np.float32))"
+ ],
+ "metadata": {
+ "id": "vQShz_vST_XM"
+ },
+ "execution_count": 13,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "BuS19IzdVfFq"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Plot original and convolved signals\\\n",
+ "plt.figure(figsize=(12, 6))\n",
+ "plt.subplot(2, 1, 1)\n",
+ "plt.title(\"Sine Wave to be convolved\")\n",
+ "plt.plot(sine_wave)\n",
+ "plt.figure(figsize=(12, 6))\n",
+ "plt.subplot(2, 1, 1)\n",
+ "plt.title(\"Original Gunshot Waveform\")\n",
+ "plt.plot(gunshot_wave)\n",
+ "plt.subplot(2, 1, 2)\n",
+ "plt.title(\"Convolved Waveform\")\n",
+ "plt.plot(convolved_wave)\n",
+ "plt.tight_layout()\n",
+ "plt.show()\n",
+ "\n",
+ "# Play the convolved audio (requires IPython.display)\n",
+ "from IPython.display import Audio\n",
+ "Audio('convolved_output.wav')"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 855
+ },
+ "id": "3pot3Y52UjB5",
+ "outputId": "716b4463-7d1e-4fcc-aa37-eb497435c88c"
+ },
+ "execution_count": 16,
+ "outputs": [
+ {
+ "output_type": "display_data",
+ "data": {
+ "text/plain": [
+ "