← All examples
74

Canonical Grid model

Physics starter - Spectrum

Compare sampling at 128 and 32 Hz; observe aliasing and verify integrated spectral power.

Scale
Small
Source
physics-spectrum.grid
Length
44 lines
Collection
Advanced language
Level
Intermediate
Runtime
Portable
Version
1.0.0
physics-spectrum.grid
Get Grid
MODEL "Physics starter - Spectrum"
DESCRIPTION "Compare sampling at 128 and 32 Hz; observe aliasing and verify integrated spectral power."
VERSION "1.0.0"
AUTHOR "Grid Team"
TAGS "physics", "starter", "spectrum"

# 128 samples; duration is one second at 128 Hz and four seconds at 32 Hz. Two exactly bin-centered real sinusoids.
input SampleRate = 128
  VALIDATE IN [32, 128] MESSAGE "Choose 32 or 128 Hz. Other rates require a leakage-aware power extraction model."
# Demonstration controls: 128 Hz resolves both tones; 32 Hz aliases 20 Hz to 12 Hz.
output ObservedSecondFrequency = ABS(MOD(20+SampleRate/2,SampleRate)-SampleRate/2)
Signal = MAKEARRAY(1,128,(row,col) => 2*SIN(2*PI()*8*(col-1)/SampleRate)+0.5*SIN(2*PI()*20*(col-1)/SampleRate))
# Columns: frequency (Hz), then one-sided power density (signal squared per Hz).
output PowerSpectrum = SIGNAL.PSD(Signal,SampleRate,"none")
output FirstToneDensity = INDEX(PowerSpectrum,ROUND(8*128/SampleRate,0)+1,2)
output SecondToneDensity = INDEX(PowerSpectrum,ROUND(ABS(MOD(20+SampleRate/2,SampleRate)-SampleRate/2)*128/SampleRate,0)+1,2)
output TotalSpectralPower = SUM(MAP(SEQUENCE(65),(row) => INDEX(PowerSpectrum,row,2)))*SampleRate/128
output ExactMeanSquare = (2^2+0.5^2)/2

# Scientific checks and interpretation
output PowerError = ABS(TotalSpectralPower - ExactMeanSquare)

output Assumptions = "Compare sampling at 128 and 32 Hz; observe aliasing and verify integrated spectral power. Solver completion alone does not establish global accuracy or physical validity."
output ScenarioGuide = "Save a Baseline input scenario, change an admitted input, inspect numerical checks and the unsmoothed plot, then restore Baseline. Save and reopen a .gridoc archive. Plot selection also accepts the PowerSpectrum numeric array; choose its X and Y columns in the chart dialog."

Charts_1!type = "charts"
Charts_1!config = <toml>
  version = 1
  kind = "charts"
  title = "Spectrum"
  [layout]
  x_axis = "Frequency (Hz)"
  y_axis = "Power spectral density"
  [[charts]]
  type = "line"
  smooth = false
  bind = "PowerSpectrum"
  x = "0"
  y = "1"
  label = "Rectangular-window PSD"
  color = "#2563eb"
</toml>

END MODEL