Skip to content

Instantly share code, notes, and snippets.

@efekarakus
Last active March 4, 2016 18:20
Show Gist options
  • Save efekarakus/3c30061ef9e56c2328c6 to your computer and use it in GitHub Desktop.
Save efekarakus/3c30061ef9e56c2328c6 to your computer and use it in GitHub Desktop.
Ricker Wavelet
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.d3_peaks = global.d3_peaks || {})));
}(this, function (exports) { 'use strict';
/**
* See https://en.wikipedia.org/wiki/Mexican_hat_wavelet
*/
function ricker() {
var σ = 1;
var ricker = function(t) {
var t2 = t*t,
variance = σ*σ;
var C = 2.0 / ( Math.sqrt(3 * σ) * (Math.pow(Math.PI, 0.25)) );
var norm = (1.0 - (t2)/(variance));
var gauss = Math.exp( -(t2) / (2*variance) );
return C*norm*gauss;
}
ricker.std = function(_) {
return arguments.length ? (σ = _, ricker) : σ;
}
return ricker;
};
var version = "0.0.1";
exports.version = version;
exports.ricker = ricker;
}));
<!DOCTYPE html>
<meta charset="utf-8">
<title>Ricker Wavelet</title>
<style>
path.wavelet {
fill: none;
stroke: black;
stroke-width: 1.5px;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="d3-peaks.js" charset="utf-8"></script>
<script>
var data = d3.range(-10, 11);
var width = 800,
height = 470,
margin = {top: 10, left: 30, bottom: 10, right: 10};
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
var g = svg.append("g")
.datum(data)
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var ricker = d3_peaks.ricker();
ricker.std(1.5);
var x = d3.scale.linear()
.domain([-10, 10])
.range([20, width]);
var y = d3.scale.linear()
.domain([-.5, 1.0])
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.tickValues(data);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.interpolate("cardinal")
.x(function(d) { return x(d); })
.y(function(d) { return y(ricker(d)); });
g.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + y(0) + ")")
.call(xAxis);
g.append("g")
.attr("class", "axis")
.attr("transform", "translate(0,0)")
.call(yAxis);
g.append("path")
.attr("d", line)
.attr("class", "wavelet");
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment