{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Evaluating Speech Quality with PESQ metric\n\nThis notebook will guide you through calculating the Perceptual Evaluation of Speech Quality (PESQ) score,\n a key metric in assessing how effective noise reduction and enhancement techniques are in improving speech quality.\n PESQ is widely adopted in industries such as telecommunications, VoIP, and audio processing.\n It provides an objective way to measure the perceived quality of speech signals from a human listener's perspective.\n\nImagine being on a noisy street, trying to have a phone call. The technology behind the scenes aims\n to clean up your voice and make it sound clearer on the other end. But how do engineers measure that improvement?\n This is where PESQ comes in. In this notebook, we will simulate a similar scenario, applying a simple noise reduction\n technique and using the PESQ score to evaluate how much the speech quality improves.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import necessary libraries\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\nimport torch\nimport torchaudio\nfrom torchmetrics.audio import PerceptualEvaluationSpeechQuality" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Generate Synthetic Clean and Noisy Audio Signals\nWe'll generate a clean sine wave (representing a clean speech signal) and add white noise to simulate the noisy version.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def generate_sine_wave(frequency, duration, sample_rate, amplitude: float = 0.5):\n \"\"\"Generate a clean sine wave at a given frequency.\"\"\"\n t = torch.linspace(0, duration, int(sample_rate * duration))\n return amplitude * torch.sin(2 * np.pi * frequency * t)\n\n\ndef add_noise(waveform: torch.Tensor, noise_factor: float = 0.05) -> torch.Tensor:\n \"\"\"Add white noise to a waveform.\"\"\"\n noise = noise_factor * torch.randn(waveform.size())\n return waveform + noise\n\n\n# Parameters for the synthetic audio\nsample_rate = 16000 # 16 kHz typical for speech\nduration = 3 # 3 seconds of audio\nfrequency = 440 # A4 note, can represent a simple speech-like tone\n\n# Generate the clean sine wave\nclean_waveform = generate_sine_wave(frequency, duration, sample_rate)\n\n# Generate the noisy waveform by adding white noise\nnoisy_waveform = add_noise(clean_waveform)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Apply Basic Noise Reduction Technique\nIn this step, we apply a simple spectral gating method for noise reduction using torchaudio's\n`spectrogram` method. This is to simulate the enhancement of noisy speech.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def reduce_noise(noisy_signal: torch.Tensor, threshold: float = 0.2) -> torch.Tensor:\n \"\"\"Basic noise reduction using spectral gating.\"\"\"\n # Compute the spectrogram\n spec = torchaudio.transforms.Spectrogram()(noisy_signal)\n\n # Apply threshold-based gating: values below the threshold will be zeroed out\n spec_denoised = spec * (spec > threshold)\n\n # Convert back to the waveform\n return torchaudio.transforms.GriffinLim()(spec_denoised)\n\n\n# Apply noise reduction to the noisy waveform\nenhanced_waveform = reduce_noise(noisy_waveform)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Initialize the PESQ Metric\nPESQ can be computed in two modes: 'wb' (wideband) or 'nb' (narrowband).\nHere, we are using 'wb' mode for wideband speech quality evaluation.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pesq_metric = PerceptualEvaluationSpeechQuality(fs=sample_rate, mode=\"wb\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compute PESQ Scores\nWe will calculate the PESQ scores for both the noisy and enhanced versions compared to the clean signal.\nThe PESQ scores give us a numerical evaluation of how well the enhanced speech\ncompares to the clean speech. Higher scores indicate better quality.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pesq_noisy = pesq_metric(clean_waveform, noisy_waveform)\npesq_enhanced = pesq_metric(clean_waveform, enhanced_waveform)\n\nprint(f\"PESQ Score for Noisy Audio: {pesq_noisy.item():.4f}\")\nprint(f\"PESQ Score for Enhanced Audio: {pesq_enhanced.item():.4f}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Visualize the waveforms\nWe can visualize the waveforms of the clean, noisy, and enhanced audio to see the differences.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, axs = plt.subplots(3, 1, figsize=(12, 9))\n\n# Plot clean waveform\naxs[0].plot(clean_waveform.numpy())\naxs[0].set_title(\"Clean Audio Waveform (Sine Wave)\")\naxs[0].set_xlabel(\"Time\")\naxs[0].set_ylabel(\"Amplitude\")\n\n# Plot noisy waveform\naxs[1].plot(noisy_waveform.numpy(), color=\"orange\")\naxs[1].set_title(f\"Noisy Audio Waveform (PESQ: {pesq_noisy.item():.4f})\")\naxs[1].set_xlabel(\"Time\")\naxs[1].set_ylabel(\"Amplitude\")\n\n# Plot enhanced waveform\naxs[2].plot(enhanced_waveform.numpy(), color=\"green\")\naxs[2].set_title(f\"Enhanced Audio Waveform (PESQ: {pesq_enhanced.item():.4f})\")\naxs[2].set_xlabel(\"Time\")\naxs[2].set_ylabel(\"Amplitude\")\n\n# Adjust layout for better visualization\nfig.tight_layout()\nplt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.20" } }, "nbformat": 4, "nbformat_minor": 0 }