Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container">
  <div class="column">
    <h3>Filter Parameters:</h3>
    <div class="slider">
      <label for="fs3">Alpha: </label>
      <input type="range" id="fs3" min="0" max="1" step="0.001" value="1">
      <span id="f3">1</span>
    </div>
    <div class="slider">
      <label for="fs4">Order: </label>
      <input type="range" id="fs4" min="1" max="10" step="1" value="1">
      <span id="f4">1</span>
    </div>
  </div>
</div>
<div>
  <canvas id="canvas" width="600" height="400"></canvas>
</div>
<h3>Filter Algorithm:</h3>
<div>
  <label for="textInput">outputSamples[n] = </label>
  <input type="text" id="textInput" value="alpha * inputSamples[n-1] + (1.0 - alpha) * outputSamples[n-1]" style="width: 500px;">
  <button id="updateButton">Update Graph</button>
</div>
<button onclick="setFilterUserString('alpha * inputSamples[n-1] + (1.0 - alpha) * outputSamples[n-1]')">Set to Lowpass</button>
<button onclick="setFilterUserString('(1.0 - alpha) * (inputSamples[n] - inputSamples[n-1] + outputSamples[n-1])')">Set to Highpass</button>
              
            
!

CSS

              
                body {
  font-family: Arial, sans-serif;
  text-align: left;
  padding: 20px;
}
canvas {
  border: 1px solid black;
  margin-top: 20px;
}

.container {
  display: flex; /* Flexbox layout */
  justify-content: space-between; /* Space between columns */
  padding: 10px;
}

.column {
  flex: 1; /* Each column takes equal width */
  padding: 5px;
  margin: 5px;
  background-color: #f2f2f2;
  border: 1px solid #ccc;
  text-align: center;
}

.slider{
  float:down;
}

span {
  display: inline-block; /* Ensures the label takes up consistent space */
  width: 50px; /* Fixed width */
}

input{
  width: 30%;
}

              
            
!

JS

              
                const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const slider3 = document.getElementById('fs3');
const slider4 = document.getElementById('fs4');

const alphaValue = document.getElementById('f3');
const filterOrderValue = document.getElementById('f4');

userString = "alpha*inputSamples[n-1] + (1.0-alpha)*outputSamples[n-1]";

slider3.addEventListener('input', function() {
  alphaValue.textContent = slider3.value;
  redraw();
});

slider4.addEventListener('input', function() {
  filterOrderValue.textContent = slider4.value;
  redraw();
});


document.getElementById('updateButton').addEventListener('click', function() {
  updateAndRedraw();
});

function setFilterUserString(str){
  document.getElementById('textInput').value = `${str}`;
  updateAndRedraw();
}

function updateAndRedraw(){
  const userInput = document.getElementById('textInput').value;
  userString = userInput;
  redraw();
}
////////////////////////
//STUFF FOR THE TUTORIAL
////////////////////////

FS = 20000 //Hz
DT = 1.0/FS //Seconds
N_SAMPLES = 512

function wave(t, frequency, amplitude, phaseShift, yOffset){
  return amplitude * Math.sin((2 * Math.PI * frequency * t) + phaseShift) + yOffset;
}

function drawFunction(func, strokestyle) {
  
  const funcScaled = (x) => -func(x)*canvas.height/2 + canvas.height/2;
  
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.beginPath();
  
  ctx.moveTo(0, funcScaled(0));
  
  
  for (let n = 0; n < N_SAMPLES; n++) {
    const y = funcScaled(n);
    ctx.lineTo(n*canvas.width/(N_SAMPLES-1), y);
  }
  
  ctx.strokeStyle = strokestyle;
  ctx.lineWidth = 2;
  ctx.stroke();
}


//This is an example low-pass filter function. 
//Here alpha sets freq
function simpleLowPassFilter(inputSamples, alpha){
  
  const nSamples = inputSamples.length;
  
  let outputSamples = new Array(inputSamples.length).fill(0);
  
  //This is our core filter algorithm. This loop is just taking the rolling average of our input function
  for(let n = 0; n < nSamples-1; n++){
    outputSamples[n] = (1.0-alpha)*outputSamples[n] + alpha*inputSamples[n];
  }
  
  return outputSamples;
  
}

//This is the actual filter that takes user input
function userFilter(userString, inputSamples, alpha){
  
  const nSamples = inputSamples.length;
  
  userStringAugmented = "return " + userString + ";";
  
  const userFunc = new Function("inputSamples", "outputSamples", "alpha", "n", userStringAugmented)
  
  let outputSamples = new Array(inputSamples.length).fill(0);
  
  for(let n = 1; n < nSamples; n++){
    outputSamples[n] = userFunc(inputSamples, outputSamples, alpha, n);
  }
  
  return outputSamples;
  
}

function redraw(){
  //drawFunction((n)=>wave(n*DT,parseFloat(slider1.value), .5, 0,0) + wave(n*DT,parseFloat(slider2.value), .5, 0,0),'black');
  let ogSamples = new Array(N_SAMPLES).fill(0);
  
  //Fill our buffer w samples
  for(let n = 0; n < N_SAMPLES; n++){
    ogSamples[n] = wave(n*DT,80, .5, 0,0) + wave(n*DT,1000, .5, 0,0) ;
  }
  
  //First order filter of input samples
  filteredSamples = userFilter(userString,ogSamples, parseFloat(slider3.value));
  
  //For higher order filters, we just apply the first order filter to the filtered samples many times
  //This should perform similarly to an nth order RC ladder filter in electronics
  for(let no = 1; no < parseInt(slider4.value); no++){
    filteredSamples = userFilter(userString,filteredSamples, parseFloat(slider3.value));
  }
  
  const ftodraw = (n) => (n >= 0 && n < filteredSamples.length) ? filteredSamples[n] : 0;
  
  drawFunction(ftodraw, 'black');
  
}

// Initial drawing
redraw();
              
            
!
999px

Console