Skip to content

Instantly share code, notes, and snippets.

@fjenner
Created February 28, 2017 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fjenner/2acd9a3ff024601e2f1a9b50d82f7721 to your computer and use it in GitHub Desktop.
Save fjenner/2acd9a3ff024601e2f1a9b50d82f7721 to your computer and use it in GitHub Desktop.
FIR Filter Demo
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:207c5d1861ebc605df545c187b4860c194ebe49af52b386b0b8926d88f89af51"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Demo Signals for FIR Filtering Demo\n",
"## Import some functions to create the audio signals"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%run NoteGen.ipynb"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 36
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create some melodies"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Super Mario Overworld Theme\n",
"overworld = [\n",
" (['D3', 'F#4', 'E5'], 0.25),\n",
" (['D3', 'F#4', 'E5'], 0.5),\n",
" (['D3', 'F#4', 'E5'], 0.5),\n",
" (['D3', 'F#4', 'C5'], 0.25),\n",
" (['D3', 'F#4', 'E5'], 0.5),\n",
" (['G3', 'B4', 'G5'], 0.5),\n",
" ([], 0.5),\n",
" (['G3', 'G4'], 0.5)\n",
"]\n",
"\n",
"# Twinkle, Twinkle Little Star\n",
"twinkle = [\n",
" (['C6'], 0.25),\n",
" (['C6'], 0.25),\n",
" (['G6'], 0.25),\n",
" (['G6'], 0.25),\n",
" (['A6'], 0.25),\n",
" (['A6'], 0.25),\n",
" (['G6'], 0.25),\n",
" ([], 0.25),\n",
" (['F6'], 0.25),\n",
" (['F6'], 0.25),\n",
" (['E6'], 0.25),\n",
" (['E6'], 0.25),\n",
" (['D6'], 0.25),\n",
" (['D6'], 0.25),\n",
" (['C6'], 0.5),\n",
"]\n",
"\n",
"# Bach's Toccata and Fugue in D minor\n",
"toccata = [\n",
" (['A7'], 0.125),\n",
" (['G7'], 0.125),\n",
" (['A7'], 0.125),\n",
" (['F7'], 0.125),\n",
" (['A7'], 0.125),\n",
" (['E7'], 0.125),\n",
" (['A7'], 0.125),\n",
" (['D7'], 0.125),\n",
" (['A7'], 0.125),\n",
" (['C#7'], 0.125),\n",
" (['A7'], 0.125),\n",
" (['D7'], 0.125),\n",
" (['A7'], 0.125),\n",
" (['E7'], 0.125),\n",
" (['A7'], 0.125),\n",
" (['F7'], 0.125),\n",
" (['A7'], 0.125),\n",
" (['A6'], 0.125),\n",
" (['A7'], 0.125),\n",
" (['B67'], 0.125),\n",
" (['A7'], 0.125),\n",
" (['C#7'], 0.125),\n",
" (['A7'], 0.125),\n",
" (['D7'], 0.125),\n",
" (['A7'], 0.125),\n",
" (['C#7'], 0.125),\n",
" (['A7'], 0.125),\n",
" (['D7'], 0.125),\n",
" (['A7'], 0.125),\n",
" (['E7'], 0.125),\n",
"]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 37
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Turn the melodies into digital signals"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Object for grouping together melody information\n",
"class Melody:\n",
" def __init__(self, name, notes, bpm=120, transpose=0, scale=1):\n",
" self.name = name\n",
" self.notes = notes\n",
" self.bpm = bpm\n",
" self.transpose = transpose\n",
" self.scale = scale\n",
" self.freqs = [note_freq(note_name, transpose) for step in notes for note_name in step[0]]\n",
" \n",
" def generate(self, fs):\n",
" return generate_waveform(self.notes, fs, self.bpm, self.transpose) * self.scale\n",
"\n",
"melodies = [\n",
" Melody(\"Channel 1\", overworld, 100, 0, 1),\n",
" Melody(\"Channel 2\", twinkle, 80, -2, 0.25),\n",
" Melody(\"Channel 3\", toccata, 90, 1, 0.125)\n",
"]\n",
"\n",
"fs = 8000\n",
"\n",
"# Create waveforms for each of the melodies\n",
"signals = [melody.generate(fs) for melody in melodies]\n",
"\n",
"# Create a composite signal that contains all of the melodies\n",
"composite = zeros(max([len(signal) for signal in signals]))\n",
"for signal in signals:\n",
" composite[:len(signal)] += signal\n",
"composite /= max(composite)\n",
"\n",
"input_signal = composite"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 38
}
],
"metadata": {}
}
]
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment