# upsidedownlabs/cortex architecture

> Architecture diagram of the GitHub repository upsidedownlabs/cortex, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-02-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/upsidedownlabs/cortex
- Repository: https://github.com/upsidedownlabs/cortex

## Overview

##### Project Type and Purpose
- CortEX is a full-stack (primarily front-end) Next.js web application for real-time neurofeedback during meditation. It connects via Web Bluetooth to a BLE-enabled biopotential device (EEG/ECG), performs real-time signal processing in browser Web Workers, classifies mental and physical states, renders rich visualizations, and allows session management and export.

##### File Structure Analysis
- Top-level:
- .github/workflows: CI for Next.js deployment
- next.config.ts, package.json, tsconfig.json: Next.js/TypeScript config
- public: static assets
- src/app: Next.js routed pages (layout.tsx, page.tsx) and global styles
- src/components: React UI components (Bledata, Dataplotting, Hrvwebglplot, MeditationSession, etc.)
- src/components/ui: Radix/Tailwind UI primitives (accordion, dialog, button…)
- src/components/SessionExport: PDF export logic
- src/lib: core JS/TS modules for filtering (fft.ts, filters.tsx, notchfilter.ts), stateClassifier.ts, utils.ts
- src/webworker: three worker entry points (bandPower.worker.ts, bpm.worker.ts, dataProcessor.worker.ts)
- Quote sources, type declarations, linter/postcss configs

##### README Insights
- Real-time EEG/ECG via BLE, visualized with WebGL (webgl-plot) and Recharts
- Signal pipeline lives in Web Workers
- Classification algos (rule-based or ML) operate on band-power data
- Session logging: durations, band power averages, HRV metrics
- Next.js for SSR/static export, Tailwind CSS for styling, Radix UI

##### Main Components
a. BLE Device (NPG Lite)
b. Browser Runtime
– Web Bluetooth API module
– Web Workers
- DataProcessor Worker: bandpass/notch filters
- BandPower Worker: FFT and band-power integration
- BPM Worker: R-peak detection and HRV metrics
– Main Thread (React/Next.js)
- State management (React context/hooks)
- UI Components (charts, waveforms, state indicators)
- Session manager & export engine (PDF)
c. Next.js Server/Hosting Layer
– SSR/static hosting, optional API routes (if extended)
d. External Libraries
– webgl-plot, Recharts, Radix UI, Tailwind CSS

##### Relationships and Data Flows
1. BLE Device → Web Bluetooth API
2. Raw data → DataProcessor Worker → filtered streams
3. Filtered streams → BandPower Worker → band-power arrays
4. Filtered ECG → BPM Worker → R-peaks & HRV metrics
5. Workers send messages → Main Thread
6. Main Thread distributes data to React components → render UI
7. Session data stored in React state → Export to PDF / download
8. (Optional) Session data → Next.js API or external database

##### Architectural Patterns & Design Principles
- Pipeline pattern: chaining Web Workers for non-blocking signal processing
- Componentized UI: reusable React/Tailwind/Radix components
- Separation of concerns: filters, FFT, classification isolated in src/lib and workers
- Hook/event-driven data flow: postMessage between workers and main thread
- Single page app with server-side rendering fall-back (Next.js)

##### Technologies & Frameworks to Highlight
- Next.js & React (pages/app router)
- TypeScript
- Web Bluetooth API
- Web Workers (three distinct workers)
- webgl-plot (WebGL), Recharts (SVG charts)
- Tailwind CSS, Radix UI components
- PDF export library (custom TS in SessionExport)

## Diagram (Mermaid)

```mermaid
flowchart TD
    HD["BLE Device (NPG Lite)"]:::hardware

    subgraph "Next.js Hosting Layer"
        AS["App Shell & Routing"]:::frontend
        SA["Static Assets"]:::hosting
        CI["Next.js Config & CI"]:::hosting

        subgraph "Browser Runtime"
            BB["Web Bluetooth API"]:::frontend

            subgraph "Web Workers"
                DP["DataProcessor Worker"]:::workers
                BP["BandPower Worker"]:::workers
                BM["BPM Worker"]:::workers
            end

            subgraph "Main Thread (React/Next.js)"
                SM["State Management"]:::frontend
                SE["Session Export Engine"]:::frontend
                subgraph "UI Components"
                    DPc["Dataplotting"]:::frontend
                    HWP["HrvWebglPlot"]:::frontend
                    MW["Meditation Waveform"]:::frontend
                    SI["State Indicator"]:::frontend
                    DS["Duration Selector"]:::frontend
                    MS["Meditation Session"]:::frontend
                end
            end
        end
    end

    subgraph "Lib Modules"
        FFT["FFT Library"]:::lib
        FL["Filtering Utils"]:::lib
        NF["Notch Filter"]:::lib
        SC["State Classifier"]:::lib
        UT["Utility Functions"]:::lib
    end

    %% Data Flows
    HD -->|"raw signal"| BB
    BB -->|"raw data"| DP
    DP -->|"filtered EEG"| BP
    DP -->|"filtered ECG"| BM
    BP -->|"band-power arrays"| SM
    BM -->|"HRV metrics & R-peaks"| SM
    SM -->|"data"| DPc
    SM -->|"data"| HWP
    SM -->|"data"| MW
    SM -->|"state"| SI
    SM -->|"control"| DS
    SM -->|"session data"| MS
    SM -->|"export request"| SE

    %% Lib integrations
    FL --> DP
    NF --> DP
    FFT --> BP
    SC --> SM
    UT --> SM

    %% Click events
    click BB "https://github.com/upsidedownlabs/cortex/blob/main/src/components/Bledata.tsx"
    click DP "https://github.com/upsidedownlabs/cortex/blob/main/src/webworker/dataProcessor.worker.ts"
    click BP "https://github.com/upsidedownlabs/cortex/blob/main/src/webworker/bandPower.worker.ts"
    click BM "https://github.com/upsidedownlabs/cortex/blob/main/src/webworker/bpm.worker.ts"
    click FFT "https://github.com/upsidedownlabs/cortex/blob/main/src/lib/fft.ts"
    click FL "https://github.com/upsidedownlabs/cortex/blob/main/src/lib/filters.tsx"
    click NF "https://github.com/upsidedownlabs/cortex/blob/main/src/lib/notchfilter.ts"
    click SC "https://github.com/upsidedownlabs/cortex/blob/main/src/lib/stateClassifier.ts"
    click UT "https://github.com/upsidedownlabs/cortex/blob/main/src/lib/utils.ts"
    click AS "https://github.com/upsidedownlabs/cortex/blob/main/src/app/layout.tsx"
    click AS "https://github.com/upsidedownlabs/cortex/blob/main/src/app/page.tsx"
    click AS "https://github.com/upsidedownlabs/cortex/blob/main/src/app/globals.css"
    click SE "https://github.com/upsidedownlabs/cortex/blob/main/src/components/SessionExport/exportToPDF.ts"
    click DPc "https://github.com/upsidedownlabs/cortex/blob/main/src/components/Dataplotting.tsx"
    click HWP "https://github.com/upsidedownlabs/cortex/blob/main/src/components/Hrvwebglplot.tsx"
    click MW "https://github.com/upsidedownlabs/cortex/blob/main/src/components/MeditationWaveform.tsx"
    click MW "https://github.com/upsidedownlabs/cortex/blob/main/src/components/WebglPlotCanvas.tsx"
    click SI "https://github.com/upsidedownlabs/cortex/blob/main/src/components/StateIndicator.tsx"
    click DS "https://github.com/upsidedownlabs/cortex/blob/main/src/components/DurationSelector.tsx"
    click MS "https://github.com/upsidedownlabs/cortex/blob/main/src/components/MeditationSession.tsx"
    click CI "https://github.com/upsidedownlabs/cortex/blob/main/next.config.ts"
    click CI "https://github.com/upsidedownlabs/cortex/blob/main/.github/workflows/nextjs.yml"
    click SA "https://github.com/upsidedownlabs/cortex/tree/main/public/"

    classDef hardware fill:#cbeafe,stroke:#0b63c4,stroke-width:1px
    classDef workers fill:#fef3c7,stroke:#f59e0b,stroke-width:1px
    classDef frontend fill:#dcfce7,stroke:#16a34a,stroke-width:1px
    classDef hosting fill:#f3f4f6,stroke:#6b7280,stroke-width:1px
    classDef lib fill:#ede9fe,stroke:#7c3aed,stroke-width:1px
```
