Technical Documentation: Waveform Generation for SNN Classification

Project Repository: https://gitee.com/waterruby/ANNA.git
License: Apache License 2.0


1. Overview

This document describes the waveform generation strategy used for training a spiking neural network (SNN) on a four‑class classification task. The key challenge is to ensure that the network learns phase‑invariant features for the base sinusoidal class while preserving discriminative, phase‑independent features for the other classes.


2. Class Definitions

Class Description Mathematical form
0 Sinusoid with random phase sin ⁡ ( t abs + ϕ ) \sin(t_{\text{abs}} + \phi) sin(tabs+ϕ), ϕ ∼ U ( 0 , 2 π ) \phi \sim \mathcal{U}(0, 2\pi) ϕU(0,2π)
1 Sinusoid + squared absolute time sin ⁡ ( t abs ) + t abs 2 \sin(t_{\text{abs}}) + t_{\text{abs}}^2 sin(tabs)+tabs2
2 Sinusoid + clipped tangent sin ⁡ ( t abs ) + c l a m p ( tan ⁡ ( t abs ) , − 2 , 2 ) \sin(t_{\text{abs}}) + \mathrm{clamp}(\tan(t_{\text{abs}}), -2, 2) sin(tabs)+clamp(tan(tabs),2,2)
3 Sum of sinusoid and cosine sin ⁡ ( t abs ) + cos ⁡ ( t abs ) \sin(t_{\text{abs}}) + \cos(t_{\text{abs}}) sin(tabs)+cos(tabs)

Where t abs ∈ [ 0 , 2 π ] t_{\text{abs}} \in [0, 2\pi] tabs[0,2π] is the absolute angle (fixed for all samples).


3. Rationale

3.1 Class 0 – Random Phase Sinusoid

  • Goal: Force the network to become insensitive to phase shifts.
  • Implementation: Each sample uses a uniformly random phase ϕ \phi ϕ. Hence the underlying waveform is a pure sinusoid but its starting point varies across samples.
  • Effect: The network cannot overfit to a fixed starting point; it must learn the shape (sinusoid) independent of phase. This drastically increases the diversity of class‑0 samples (infinite variations in practice).

3.2 Classes 1, 2, 3 – Fixed Absolute Angle

  • Goal: Preserve deterministic, reproducible features that are easy to separate.
  • Why fixed angle?
    • The additive components ( t abs 2 t_{\text{abs}}^2 tabs2, tan ⁡ ( t abs ) \tan(t_{\text{abs}}) tan(tabs), and cos ⁡ ( t abs ) \cos(t_{\text{abs}}) cos(tabs)) break the phase symmetry.
    • Using an absolute angle ensures that the position of the distinctive structures (e.g., the sharp rise from t 2 t^2 t2, the spike of tan ⁡ \tan tan near π / 2 \pi/2 π/2, or the bump of cos ⁡ \cos cos) is consistent across all samples of the same class.
    • This allows the SNN to learn temporally localised features that are strongly correlated with the class label.

4. Data Generation Procedure

For each sample in a batch:

  1. Choose a random class c ∈ { 0 , 1 , 2 , 3 } c \in \{0,1,2,3\} c{0,1,2,3} uniformly.
  2. If c = 0 c = 0 c=0:
    • Draw a random phase ϕ ∼ U ( 0 , 2 π ) \phi \sim \mathcal{U}(0, 2\pi) ϕU(0,2π).
    • Compute the waveform: w ( t ) = sin ⁡ ( t abs + ϕ ) w(t) = \sin(t_{\text{abs}} + \phi) w(t)=sin(tabs+ϕ).
  3. If c = 1 c = 1 c=1:
    • w ( t ) = sin ⁡ ( t abs ) + t abs 2 w(t) = \sin(t_{\text{abs}}) + t_{\text{abs}}^2 w(t)=sin(tabs)+tabs2.
  4. If c = 2 c = 2 c=2:
    • w ( t ) = sin ⁡ ( t abs ) + c l a m p ( tan ⁡ ( t abs ) , − 2.0 , 2.0 ) w(t) = \sin(t_{\text{abs}}) + \mathrm{clamp}(\tan(t_{\text{abs}}), -2.0, 2.0) w(t)=sin(tabs)+clamp(tan(tabs),2.0,2.0).
  5. If c = 3 c = 3 c=3:
    • w ( t ) = sin ⁡ ( t abs ) + cos ⁡ ( t abs ) w(t) = \sin(t_{\text{abs}}) + \cos(t_{\text{abs}}) w(t)=sin(tabs)+cos(tabs).
  6. Normalise each waveform independently to the range [ 0 , 1 ] [0,1] [0,1] (or [ − 1 , 1 ] [-1,1] [1,1] depending on the network’s input encoding) using its own minimum and maximum:
    w norm = w − min ⁡ ( w ) max ⁡ ( w ) − min ⁡ ( w ) + ϵ w_{\text{norm}} = \frac{w - \min(w)}{\max(w) - \min(w) + \epsilon} wnorm=max(w)min(w)+ϵwmin(w)
    where ϵ \epsilon ϵ is a small constant to avoid division by zero.

5. Why This Approach Works

  • Phase‑invariance learning: By exposing class‑0 with many random phases, the network is forced to ignore phase and extract the sinusoidal shape.
  • Distinctive temporal landmarks: Classes 1–3 have features that appear at fixed absolute angles (e.g., the maximum of t 2 t^2 t2 at t = 2 π t=2\pi t=2π, the tan ⁡ \tan tan spike at π / 2 \pi/2 π/2). The SNN can use its temporal dynamics (e.g., precise spike timing) to detect these landmarks.
  • Improved generalisation: The validation and test sets are generated with the same protocol but using a fixed random seed to ensure reproducibility. During inference, the network sees previously unseen phases for class‑0, and its performance demonstrates true phase invariance.

6. Implementation Example (PyTorch)

import torch
import numpy as np

def generate_waveform_batch(batch_size, time_steps, device='cpu', fixed_seed=None):
    if fixed_seed is not None:
        np.random.seed(fixed_seed)
    t_abs = torch.linspace(0, 2*np.pi, time_steps).to(device)
    sin_abs = torch.sin(t_abs)
    cos_abs = torch.cos(t_abs)
    t2_abs = t_abs ** 2
    tan_abs = torch.tan(t_abs).clamp(-2.0, 2.0)
    sin_cos = sin_abs + cos_abs

    batch_wave = []
    batch_label = []
    for _ in range(batch_size):
        label = np.random.randint(0, 4)
        if label == 0:
            phase = np.random.uniform(0, 2*np.pi)
            wave = torch.sin(t_abs + phase)
        elif label == 1:
            wave = sin_abs + t2_abs
        elif label == 2:
            wave = sin_abs + tan_abs
        else:
            wave = sin_cos

        # Normalise each sample to [0,1]
        w_min = wave.min()
        w_max = wave.max()
        if w_max - w_min < 1e-8:
            wave = torch.zeros_like(wave)
        else:
            wave = (wave - w_min) / (w_max - w_min)
        batch_wave.append(wave)
        batch_label.append(label)

    return torch.stack(batch_wave), torch.tensor(batch_label, dtype=torch.long, device=device)

7. Expected Outcomes

  • The SNN trained with this data generation strategy should achieve >85% validation accuracy after fine‑tuning.
  • Class‑0 accuracy will be robust to different phase offsets, while classes 1–3 will be recognised by their fixed temporal features.
  • Compared to a naive approach (all classes using absolute angles), the network will not overfit to a single phase pattern and will generalise much better to real‑world audio where phase is irrelevant.

8. License

This work is licensed under the Apache License, Version 2.0. A copy of the license can be found in the repository root or at https://www.apache.org/licenses/LICENSE-2.0.

SPDX-License-Identifier: Apache-2.0


9. Repository Information

Logo

脑启社区是一个专注类脑智能领域的开发者社区。欢迎加入社区,共建类脑智能生态。社区为开发者提供了丰富的开源类脑工具软件、类脑算法模型及数据集、类脑知识库、类脑技术培训课程以及类脑应用案例等资源。

更多推荐