Number of Walls

[ ["0","0"], ["1","1"], ["2","2"], ["3","3"], ["4","4"] ]

Room Width

Room Height

Reflection Coef.

Outside Room

Amplitude Scale

Time Averaging

[ ["0","NONE"], ["1","1 ms"], ["5","5 ms"], ["10","10 ms"] ]

Ray Paths

Show/hide all

Direct

1st Order

2nd Order

3rd Order

4th Order

5th Order

color.add("wall",color.neutral) reg.wallCard.style.background = color.wall reg.wallCard.style.color = color.textInv color.add("img0","var(--sharp-red)") reg.img0Card.style.background = color.img0 reg.img0Card.style.color = "black" color.add("img1","var(--sharp-orange)") reg.img1Card.style.background = color.img1 reg.img1Card.style.color = "black" color.add("img2","var(--sharp-yellow)") reg.img2Card.style.background = color.img2 reg.img2Card.style.color = "black" color.add("img3","var(--mid-green)") reg.img3Card.style.background = color.img3 reg.img3Card.style.color = "black" color.add("img4","var(--sharp-blue)") reg.img4Card.style.background = color.img4 reg.img4Card.style.color = "black" color.add("img5","var(--sharp-purple)") reg.img5Card.style.background = color.img5 reg.img5Card.style.color = "black" const wallWeight = 8 const imgSize = 7 const pathWidth = 2 const p1Size = this.height - 300 const p1Limit = 30 const p1MinLimit = 5 const p1 = this.newPlot({ position: [this.left+100, this.height/2 - p1Size/2], size: [p1Size, p1Size], rangeX: [-10,10], rangeY: [-10,10], labelX: "Position [m]", labelY: "Position [m]", labelFontSizeX: 40, labelFontSizeY: 40, tickArrayX: sampleLin(-p1Limit,p1Limit,p1Limit+1), tickArrayY: sampleLin(-p1Limit,p1Limit,p1Limit+1), tickEndsX: false, tickEndsY: false, }) const [p1x,p1y] = p1.position const [p1w,p1h] = p1.size p1.text({ text:"Image Sources", position:[p1x+p1w/2,p1y+p1h+30], anchor:[0.5,0], screen: true, clip: false, fontSize: 50, }) p1.addAction(pl=>{ if (p1.x.max>=p1Limit) return p1.x.min -= 1 p1.x.max += 1 p1.y.min -= 1 p1.y.max += 1 p1.content.update() sourceEl.update() receiverEl.update() receiverElLow.update() wavefrontG.update() },"wheeldown") p1.addAction(pl=>{ if (p1.x.max<=p1MinLimit) return p1.x.min += 1 p1.x.max -= 1 p1.y.min += 1 p1.y.max -= 1 p1.content.update() sourceEl.update() receiverEl.update() receiverElLow.update() wavefrontG.update() },"wheelup") const timePlotHeight = this.height - 200 const p2 = this.newPlot({ position: [ this.width/2 + 120, this.height/2 - timePlotHeight/2], size: [this.width/2 - 160, timePlotHeight], rangeX: [0,reg.time.end*1e3], rangeY: [0.05,1], labelX: "Time [ms]", labelY: "Relative Amplitude", labelFontSizeX: 40, labelFontSizeY: 40, tickArrayY: [.05, .1, .2, .3, .4, .5, .6, .7, .8, .9, 1], tickEndsY:false, typeY:"log", }) const [p2x,p2y] = p2.position const [p2w,p2h] = p2.size p2.text({ text:"Impulse Response", position:[p2x+p2w/2,p2y+p2h+30], anchor:[0.5,0], screen: true, clip: false, fontSize: 50, }) reg.ampScale.onUpdate(v=>{ p2.y.type = v?"log":"lin" barG.update() }) const maxOrder = 5 const wallG = p1.group("wall") const rayPathG = p1.group("paths") const receiver = new Vec(2,-1) const receiverElLow = p1.circle({ position:()=>receiver, fill: color.plotBackground, radius: imgSize+2, stroke: "white", }) const wavefrontG = p1.group("wavefront") const imageG = p1.group("images") const imageMap = new Map() for(let oDex=maxOrder;oDex>=0;oDex--){ imageMap.set(oDex,p1.group("img"+oDex,rayPathG)) reg[`img${oDex}Vis`].onUpdate(v=>imageMap.get(oDex).hide(!v)) } let hideAll = true reg.allVis.addAction(()=>{ hideAll = !hideAll for(let oDex=maxOrder;oDex>=0;oDex--) reg[`img${oDex}Vis`].value = hideAll }) const coverG = p1.group("cover") const barG = p2.group("bars") const source = new Vec(-1,1) const sourceEl = p1.circle({ position:()=>source, fill: color.img0, radius: imgSize, group: p1.overlay, stroke:"black", strokeWidth:3, classAdd:"source cursor-grab" }) sourceEl.addAction(()=>grab(sourceEl),"grab") sourceEl.addAction(()=>release(sourceEl),"release") const receiverEl = p1.circle({ position:()=>receiver, fill: "transparent", radius: imgSize-2, group: p1.overlay, stroke: "black", strokeWidth:5, classAdd:"receiver cursor-grab", }) receiverEl.addAction(()=>grab(receiverEl),"grab") receiverEl.addAction(()=>release(receiverEl),"release") p1.addAction(pl=>{ if(sourceEl.drag){ source.x = pl.x source.y = pl.y walls.forEach(w=>{ if (!w.onRight(source)){ const nearest = w.nearest(source) source.x = nearest.x source.y = nearest.y } }) sourceEl.update() calcImages() } if(receiverEl.drag){ receiver.x = pl.x receiver.y = pl.y walls.forEach(w=>{ if (!w.onRight(receiver)){ const nearest = w.nearest(receiver) receiver.x = nearest.x receiver.y = nearest.y } }) receiverEl.update() receiverElLow.update() calcImages() } }) p1.addAction(()=>{ release(sourceEl) release(receiverEl) },"release") function grab(el){ el.drag = true el.classList.add("cursor-none") p1.overlay.classList.add("cursor-none") reg.time.pause() reg.time.time = 0 } function release(el){ el.drag = false el.classList.remove("cursor-none") p1.overlay.classList.remove("cursor-none") } let walls = [] let [wallX, wallY] = [[],[]] let [coverX, coverY] = [[],[]] const wall = p1.line({ x: ()=>wallX, y: ()=>wallY, stroke: color.wall, strokeWidth: wallWeight, strokeLinecap: "square", fill: "none", group: wallG, }) const cover = p1.line({ x: ()=>coverX, y: ()=>coverY, stroke: "none", fill: color.wall, opacity: 0.1, group: coverG, }) reg.coverVis.onUpdate(v=>cover.style.opacity = v?0.1:1) reg.wallCount.onUpdate(()=>updateWalls()) reg.roomWidth.onUpdate(()=>updateWalls()) reg.roomHeight.onUpdate(()=>updateWalls()) const W = p1Limit + 1 const H = p1Limit + 1 function updateWalls(){ const w = reg.roomWidth.value/2 const h = reg.roomHeight.value/2 switch (reg.wallCount.value){ case ("0"): walls = [] wallX = [] wallY = [] coverX = [] coverY = [] break case ("1"): walls = [ new Segment([-w,-H],[-w,H]), ] wallX = [-w,-w] wallY = [H,-H] coverX = [-W,-w,-w,-W] coverY = [H,H,-H,-H] break case ("2"): walls = [ new Segment([W,-h],[-w,-h]), new Segment([-w,-h],[-w,H]), ] wallX = [-w,-w,W] wallY = [H,-h,-h] coverX = [-W,-w,-w,W,W,-W] coverY = [H,H,-h,-h,-H,-H] break case ("3"): walls = [ new Segment([w,H],[w,-h]), new Segment([w,-h],[-w,-h]), new Segment([-w,-h],[-w,H]), ] wallX = [-w,-w,w,w] wallY = [H,-h,-h,H] coverX = [-W,-w,-w,w,w,W,W,-W] coverY = [H,H,-h,-h,H,H,-H,-H] break case ("4"): walls = [ new Segment([-w,h],[w,h]), new Segment([w,h],[w,-h]), new Segment([w,-h],[-w,-h]), new Segment([-w,-h],[-w,h]), ] wallX = [-w,-w,w,w,-w] wallY = [h,-h,-h,h,h] coverX = [-W,-w,-w,w,w,-w,-w,W,W,-W] coverY = [H,H,-h,-h,h,h,H,H,-H,-H] break } walls.forEach(w=>{ if (!w.onRight(source)){ const nearest = w.nearest(source) source.x = nearest.x source.y = nearest.y } if (!w.onRight(receiver)){ const nearest = w.nearest(receiver) receiver.x = nearest.x receiver.y = nearest.y } }) sourceEl.update() receiverEl.update() receiverElLow.update() wall.update() cover.update() calcImages() } const images = [] let t = 0 const c = 343 reg.time.onUpdate(tt=>{ t = tt wavefrontG.update() timeCover.update() }) function calcImages(){ reg.time.pause() reg.time.time = 0 images.forEach(im=>{ im.line.delete() im.wavefront.delete() im.bar.delete() if(im.order==0) return im.leader.delete() im.dot.delete() }) const path0 = new VecArray([source, receiver]) images.splice(0,images.length,{ position: source, path: path0, distance: path0.pathLength, wallOrder: [], order: 0, imgOrder: [source], dot: sourceEl, }) for(let order = 1; order<=maxOrder; order++){ walls.forEach(w=>{ images.forEach(im=>{ if (im.order!=order-1) return const position = w.reflect(im.position) if (w.onRight(position,false)) return const imgOrder = [position, ...im.imgOrder] const wallOrder = [w, ...im.wallOrder] const path = new VecArray([receiver]) let valid = true for(let woDex = 0; woDex < wallOrder.length; woDex++){ const intersection = Segment.intersect(wallOrder[woDex], new Segment(path.end, imgOrder[woDex]), false) if(!intersection){valid=false; break} path.push(intersection) } if(valid){ path.push(source) images.push({position,path,wallOrder,imgOrder,order,distance:path.pathLength}) } }) }) } images.forEach(im=>{ im.wavefront = p1.circle({ position:()=>im.position, radius:()=>p1.toXsize(t*c), fill:"none", stroke: color["img"+im.order], strokeWidth: 5, group:wavefrontG, }) const [X,Y] = im.path.toArrays() im.line = p1.line({ x:X, y:Y, stroke: color["img"+im.order], strokeWidth:pathWidth, strokeLinejoin: "bevel", group:imageMap.get(im.order), }) if (im.order==0) return im.leader = p1.line({ x:[im.position.x,receiver.x], y:[im.position.y,receiver.y], stroke: color["img"+im.order], strokeWidth:pathWidth, dashArray: "10 10", group:imageMap.get(im.order), }) im.dot = p1.circle({ position:im.position, fill:color["img"+im.order], stroke:"black", strokeWidth:3, radius:imgSize, group:imageG, }) }) updateResponse() } const timeCover = p2.line({ x:()=>[t*1e3,t*1e3], y:()=>[p2.y.min,p2.y.max], stroke:color.neutral, dash:"20 5", strokeWidth:4, }) function updateResponse(){ images.sort((a,b)=>b.distance-a.distance || b.order - a.order) const delta = +reg.timeAvg.value const timeBins = delta==0 ? [] : sampleLin(0,reg.time.end*1e3,reg.time.end*1e3/delta+1) const timeMap = new Map(timeBins.map(v=>[v,p2.y.min])) images.forEach((im,imDex)=>{ im.bar?.delete() const A = (reg.reflection.value**im.order)/im.distance let baseA = p2.y.min let baseT = im.distance/c*1e3 if(delta!=0){ baseT = delta * round(baseT/delta) baseA = timeMap.get(baseT) timeMap.set(baseT,baseA+A) } im.bar = p2.rect({ position: ()=>[baseT,baseA], size: ()=>[max(1,delta),A], anchor: ()=>[0,0], fill: color["img"+im.order], group: barG, stroke: color.plotBackground, }) im.bar.addAction(pl=>{ images.forEach((im2,imDex2)=>{ if(imDex2!=imDex){ im2.dot.style.opacity = 0.1 im2.bar.style.opacity = 0.1 im2.line.style.opacity = 0.1 im2.wavefront.style.opacity = 0.1 if(im2.leader) im2.leader.style.opacity = 0.1 } }) },"hover") im.bar.addAction(pl=>{ images.forEach((im2,imDex2)=>{ im2.dot.style.opacity = 1 im2.bar.style.opacity = 1 im2.line.style.opacity = 1 im2.wavefront.style.opacity = 1 if(im2.leader) im2.leader.style.opacity = 1 }) },"unhover") }) } reg.reflection.onUpdate(v=>updateResponse()) reg.timeAvg.onUpdate(v=>updateResponse()) updateWalls()