Spaces:
Running
Running
File size: 14,178 Bytes
79fd234 86fdb32 79fd234 86fdb32 79fd234 86fdb32 79fd234 86fdb32 79fd234 86fdb32 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Perceptron Passive Radar</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: monospace;
}
body {
background: #001;
color: #0f8;
min-height: 100vh;
overflow: hidden;
}
.main-container {
display: grid;
grid-template-columns: 1fr 400px;
gap: 10px;
padding: 10px;
height: 100vh;
}
.radar-container {
position: relative;
border: 2px solid #0f8;
border-radius: 50%;
background: radial-gradient(circle, rgba(0,255,136,0.05) 0%, rgba(0,0,0,0.9) 100%);
overflow: hidden;
}
.sweep-line {
position: absolute;
top: 50%;
left: 50%;
width: 50%;
height: 2px;
background: linear-gradient(90deg, #0f8, transparent);
transform-origin: left;
animation: radar-sweep 4s linear infinite;
}
.grid-overlay {
position: absolute;
width: 100%;
height: 100%;
background:
linear-gradient(#0f81 1px, transparent 1px),
linear-gradient(90deg, #0f81 1px, transparent 1px);
background-size: 50px 50px;
}
@keyframes radar-sweep {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.control-panel {
display: flex;
flex-direction: column;
gap: 10px;
}
.panel {
background: rgba(0,255,136,0.1);
border: 1px solid #0f8;
padding: 10px;
}
.perceptron-layer {
display: flex;
justify-content: space-between;
align-items: center;
height: 200px;
margin: 10px 0;
position: relative;
}
.layer {
display: flex;
flex-direction: column;
gap: 4px;
}
.neuron {
width: 12px;
height: 12px;
background: #0f8;
border-radius: 50%;
position: relative;
opacity: 0.3;
transition: all 0.2s;
}
.neuron.active {
opacity: 1;
box-shadow: 0 0 10px #0f8;
}
.connections {
position: absolute;
width: 100%;
height: 100%;
pointer-events: none;
}
.spectrum {
height: 120px;
display: flex;
align-items: flex-end;
gap: 2px;
}
.bar {
flex: 1;
background: #0f8;
transition: height 0.1s;
}
.signal {
position: absolute;
width: 8px;
height: 8px;
background: #0f8;
border-radius: 50%;
transform: translate(-50%, -50%);
}
.signal-echo {
position: absolute;
border: 1px solid #0f8;
border-radius: 50%;
transform: translate(-50%, -50%);
animation: echo 2s ease-out forwards;
}
@keyframes echo {
0% {
width: 8px;
height: 8px;
opacity: 1;
}
100% {
width: 60px;
height: 60px;
opacity: 0;
}
}
button {
background: transparent;
border: 1px solid #0f8;
color: #0f8;
padding: 8px;
cursor: pointer;
transition: 0.3s;
}
button:hover {
background: #0f8;
color: #001;
}
.stats {
font-size: 12px;
line-height: 1.5;
}
canvas {
margin-top: 10px;
border: 1px solid #0f8;
}
</style>
</head>
<body>
<div class="main-container">
<div class="radar-container">
<div class="grid-overlay"></div>
<div class="sweep-line"></div>
<div id="signals"></div>
</div>
<div class="control-panel">
<div class="panel">
<h3>Perceptron Network</h3>
<div class="perceptron-layer">
<canvas id="networkCanvas"></canvas>
</div>
<div class="stats" id="networkStats">
Accuracy: 0%<br>
Detections: 0<br>
Signal Strength: 0
</div>
</div>
<div class="panel">
<h3>Frequency Analysis</h3>
<div class="spectrum" id="spectrum"></div>
</div>
<div class="panel">
<button onclick="startRadar()">Start Detection</button>
<button onclick="stopRadar()">Stop</button>
<button onclick="trainPerceptrons()">Train Network</button>
<button onclick="toggleLearning()">Toggle Learning</button>
</div>
</div>
</div>
<script>
class Perceptron {
constructor(inputs) {
this.weights = new Array(inputs).fill(0).map(() => Math.random() * 2 - 1);
this.bias = Math.random() * 2 - 1;
this.learningRate = 0.1;
}
activate(sum) {
return sum > 0 ? 1 : 0;
}
predict(inputs) {
const sum = inputs.reduce((sum, input, i) => sum + input * this.weights[i], 0) + this.bias;
return this.activate(sum);
}
train(inputs, target) {
const prediction = this.predict(inputs);
const error = target - prediction;
for(let i = 0; i < this.weights.length; i++) {
this.weights[i] += error * inputs[i] * this.learningRate;
}
this.bias += error * this.learningRate;
return Math.abs(error);
}
}
class PerceptronNetwork {
constructor(inputSize, hiddenSize, outputSize) {
this.inputLayer = new Array(hiddenSize)
.fill(0)
.map(() => new Perceptron(inputSize));
this.outputLayer = new Array(outputSize)
.fill(0)
.map(() => new Perceptron(hiddenSize));
}
predict(inputs) {
const hiddenOutputs = this.inputLayer.map(p => p.predict(inputs));
return this.outputLayer.map(p => p.predict(hiddenOutputs));
}
train(inputs, targets) {
const hiddenOutputs = this.inputLayer.map(p => p.predict(inputs));
const finalOutputs = this.outputLayer.map(p => p.predict(hiddenOutputs));
let error = 0;
this.outputLayer.forEach((p, i) => {
error += p.train(hiddenOutputs, targets[i]);
});
this.inputLayer.forEach(p => {
error += p.train(inputs, 1);
});
return error / (this.inputLayer.length + this.outputLayer.length);
}
}
// Initialize system
const INPUT_SIZE = 32;
const HIDDEN_SIZE = 16;
const OUTPUT_SIZE = 4;
let isRunning = false;
let isLearning = false;
let network = new PerceptronNetwork(INPUT_SIZE, HIDDEN_SIZE, OUTPUT_SIZE);
let audioContext, analyser, dataArray;
// Setup UI
const spectrum = document.getElementById('spectrum');
for(let i = 0; i < INPUT_SIZE; i++) {
const bar = document.createElement('div');
bar.className = 'bar';
spectrum.appendChild(bar);
}
// Setup canvas
const canvas = document.getElementById('networkCanvas');
canvas.width = 350;
canvas.height = 180;
const ctx = canvas.getContext('2d');
function drawNetwork(inputs, hiddenOutputs, finalOutputs) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = '#0f8';
ctx.lineWidth = 0.5;
const drawLayer = (nodes, x, active) => {
const spacing = canvas.height / (nodes.length + 1);
return nodes.map((value, i) => {
const y = spacing * (i + 1);
ctx.beginPath();
ctx.arc(x, y, 4, 0, Math.PI * 2);
ctx.fillStyle = `rgba(0,255,136,${active ? value : 0.3})`;
ctx.fill();
return {x, y};
});
};
const inputNodes = drawLayer(inputs, 30, true);
const hiddenNodes = drawLayer(hiddenOutputs, canvas.width/2, true);
const outputNodes = drawLayer(finalOutputs, canvas.width - 30, true);
// Draw connections
ctx.beginPath();
inputNodes.forEach(input => {
hiddenNodes.forEach(hidden => {
ctx.moveTo(input.x, input.y);
ctx.lineTo(hidden.x, hidden.y);
});
});
hiddenNodes.forEach(hidden => {
outputNodes.forEach(output => {
ctx.moveTo(hidden.x, hidden.y);
ctx.lineTo(output.x, output.y);
});
});
ctx.stroke();
}
async function startRadar() {
try {
if(!audioContext) {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const source = audioContext.createMediaStreamSource(stream);
analyser = audioContext.createAnalyser();
analyser.fftSize = 64;
source.connect(analyser);
dataArray = new Uint8Array(analyser.frequencyBinCount);
}
isRunning = true;
updateRadar();
} catch(err) {
console.error('Error accessing microphone:', err);
}
}
function stopRadar() {
isRunning = false;
}
function updateRadar() {
if(!isRunning) return;
analyser.getByteFrequencyData(dataArray);
// Update spectrum
const bars = spectrum.children;
for(let i = 0; i < bars.length; i++) {
const height = (dataArray[i] / 255) * 120;
bars[i].style.height = height + 'px';
}
// Normalize input data
const inputs = Array.from(dataArray).map(x => x / 255);
// Get network predictions
const hiddenOutputs = network.inputLayer.map(p => p.predict(inputs));
const outputs = network.outputLayer.map(p => p.predict(hiddenOutputs));
// Draw network state
drawNetwork(inputs, hiddenOutputs, outputs);
// Create signals for strong predictions
outputs.forEach((output, i) => {
if(output > 0.7) {
createSignal(i);
}
});
// Update stats
const avgOutput = outputs.reduce((a,b) => a+b) / outputs.length;
document.getElementById('networkStats').innerHTML =
`Accuracy: ${Math.round(avgOutput * 100)}%<br>` +
`Detections: ${outputs.filter(x => x > 0.7).length}<br>` +
`Signal Strength: ${Math.round(inputs.reduce((a,b) => a+b) / inputs.length * 100)}`;
if(isLearning) {
const target = Array(OUTPUT_SIZE).fill(0);
target[Math.floor(Math.random() * OUTPUT_SIZE)] = 1;
network.train(inputs, target);
}
requestAnimationFrame(updateRadar);
}
function createSignal(type) {
const radar = document.querySelector('.radar-container');
const angle = Math.random() * Math.PI * 2;
const distance = Math.random() * (radar.offsetWidth / 3);
const x = Math.cos(angle) * distance + radar.offsetWidth / 2;
const y = Math.sin(angle) * distance + radar.offsetHeight / 2;
const signal = document.createElement('div');
signal.className = 'signal';
signal.style.left = x + 'px';
signal.style.top = y + 'px';
radar.appendChild(signal);
const echo = document.createElement('div');
echo.className = 'signal-echo';
echo.style.left = x + 'px';
echo.style.top = y + 'px';
radar.appendChild(echo);
setTimeout(() => {
signal.remove();
echo.remove();
}, 2000);
}
function trainPerceptrons() {
let error = 0;
for(let i = 0; i < 100; i++) {
const inputs = Array(INPUT_SIZE).fill(0).map(() => Math.random());
const targets = Array(OUTPUT_SIZE).fill(0);
targets[Math.floor(Math.random() * OUTPUT_SIZE)] = 1;
error += network.train(inputs, targets);
}
document.getElementById('networkStats').innerHTML =
`Training complete<br>Error: ${Math.round(error * 100) / 100}<br>Network ready`;
}
function toggleLearning() {
isLearning = !isLearning;
}
</script>
</body>
</html> |