Commit d831c98d authored by Leigh Smith's avatar Leigh Smith

Now uses Centroid calculation, retained the old calculation

parent 22d94ff4
{ {
"metadata": { "metadata": {
"name": "", "name": "",
"signature": "sha256:620336a15b319b335fbc26f830d5f6b5507392500835305cf3d7fd579934ff6e" "signature": "sha256:b4779de8997f272445c84c210f3048f03d085c9dd003e7005e82662dc0906c79"
}, },
"nbformat": 3, "nbformat": 3,
"nbformat_minor": 0, "nbformat_minor": 0,
...@@ -211,11 +211,12 @@ ...@@ -211,11 +211,12 @@
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"from essentia.standard import ZeroCrossingRate, CentralMoments, Spectrum, Windowing\n", "from essentia.standard import ZeroCrossingRate, CentralMoments, Spectrum, Windowing, Centroid\n",
"zcr = ZeroCrossingRate()\n", "zcr = ZeroCrossingRate()\n",
"hamming_window = Windowing(type = 'hamming') # we need to window the frame to avoid FFT artifacts.\n", "hamming_window = Windowing(type = 'hamming') # we need to window the frame to avoid FFT artifacts.\n",
"spectrum = Spectrum()\n", "spectrum = Spectrum()\n",
"central_moments = CentralMoments()\n", "central_moments = CentralMoments()\n",
"spectral_centroid = Centroid()\n",
"\n", "\n",
"frame_size = 0.100 * sample_rate # 100ms\n", "frame_size = 0.100 * sample_rate # 100ms\n",
"file_index = 0 # to process a single file, this will be your file loop iteration\n", "file_index = 0 # to process a single file, this will be your file loop iteration\n",
...@@ -223,15 +224,65 @@ ...@@ -223,15 +224,65 @@
"current_frame = audio[0 : frame_size]\n", "current_frame = audio[0 : frame_size]\n",
"features_snare[file_index, 0] = zcr(current_frame)\n", "features_snare[file_index, 0] = zcr(current_frame)\n",
"spectral_magnitude = spectrum(hamming_window(current_frame))\n", "spectral_magnitude = spectrum(hamming_window(current_frame))\n",
"centroid = spectral_centroid(spectral_magnitude)\n",
"\n", "\n",
"# Spectral centroid in normalised bandwidth (i.e fraction of Nyquist frequency).\n",
"centroid"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 41,
"text": [
"0.22001729905605316"
]
}
],
"prompt_number": 41
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Centroid in spectral coefficient indices.\n",
"centroid * spectral_magnitude.shape[0]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 42,
"text": [
"485.3581617176533"
]
}
],
"prompt_number": 42
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Essentia can make things seem a little magical, here is how the centroid would be calculated:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Normalize the spectral magnitude\n", "# Normalize the spectral magnitude\n",
"norm_spectral_mag = spectral_magnitude / np.sum(spectral_magnitude);\n", "norm_spectral_mag = spectral_magnitude / np.sum(spectral_magnitude)\n",
"# Make index vector\n", "# Make index vector\n",
"indices = np.arange(spectral_magnitude.shape[0])\n", "indices = np.arange(norm_spectral_mag.shape[0])\n",
"\n", "\n",
"# Centroid, in spectral coefficient indices.\n", "# Centroid, in spectral coefficient indices.\n",
"centroid = np.sum(norm_spectral_mag * indices)\n", "my_centroid = np.sum(norm_spectral_mag * indices)\n",
"centroid" "my_centroid"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
...@@ -239,13 +290,13 @@ ...@@ -239,13 +290,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 23, "prompt_number": 50,
"text": [ "text": [
"485.13728477961195" "485.13728477961195"
] ]
} }
], ],
"prompt_number": 23 "prompt_number": 50
}, },
{ {
"cell_type": "code", "cell_type": "code",
...@@ -266,13 +317,13 @@ ...@@ -266,13 +317,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 32, "prompt_number": 51,
"text": [ "text": [
"array([ 1. , 0. , 0.04079479, 0.00723291, 0.00497805], dtype=float32)" "array([ 1. , 0. , 0.04079479, 0.00723291, 0.00497805], dtype=float32)"
] ]
} }
], ],
"prompt_number": 32 "prompt_number": 51
}, },
{ {
"cell_type": "code", "cell_type": "code",
...@@ -289,14 +340,13 @@ ...@@ -289,14 +340,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 34, "prompt_number": 52,
"text": [ "text": [
"array([ 1.29251704e-01, 4.85137285e+02, 4.07947935e-02,\n", "array([ 0.1292517 , 0.2200173 , 0.04079479, 0.00723291, 0.00497805])"
" 7.23291235e-03, 4.97804629e-03])"
] ]
} }
], ],
"prompt_number": 34 "prompt_number": 52
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment