-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
177 lines (152 loc) · 6.17 KB
/
Copy pathapp.py
File metadata and controls
177 lines (152 loc) · 6.17 KB
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
import streamlit as st
from src.ui import render_sidebar, render_header, render_status_panel
from src.processing import run_signal_simulation, run_audio_simulation
from src.plots import (
build_time_domain_figure,
build_reconstruction_figure,
build_frequency_figure,
build_naive_vs_filtered_figure,
build_audio_waveform_figure,
build_spectrum_overlay_figure,
build_audio_naive_vs_filtered_figure,
)
from src.audio_utils import audio_bytes_from_signal, read_uploaded_audio
from src.config import APP_TITLE
st.set_page_config(page_title=APP_TITLE, layout="wide", page_icon="🌊")
render_header()
mode, settings = render_sidebar()
# ══════════════════════════════════════════════════════
# SYNTHETIC SIGNAL MODE
# ══════════════════════════════════════════════════════
if mode == "Synthetic Signal":
result = run_signal_simulation(settings)
# === STATUS METRICS ===
render_status_panel(result)
# === EXPLANATION LAYER ===
with st.expander("📊 Methodology & Analysis", expanded=True):
st.markdown(result["explanation"])
st.divider()
# === VIEW 1 & 2: Time-domain (side by side) ===
st.subheader("📈 Time-Domain Views")
col1, col2 = st.columns(2)
with col1:
st.plotly_chart(
build_time_domain_figure(result, theme=settings["theme"]),
use_container_width=True
)
with col2:
st.plotly_chart(
build_reconstruction_figure(result, theme=settings["theme"]),
use_container_width=True
)
st.divider()
# === VIEW 3: Frequency domain ===
st.subheader("🎵 Frequency-Domain View (FFT)")
st.plotly_chart(
build_frequency_figure(result, theme=settings["theme"]),
use_container_width=True
)
st.divider()
# === VIEW 4: THE KEY CHART — Naive vs Anti-Aliased ===
st.subheader("⚖️ Comparative Analysis: Naive vs Anti-Aliased Downsampling")
st.caption(
"**Left:** Naive decimation — takes every N-th sample with NO filtering. "
"High-frequency components fold back (alias) into the low-frequency range. | "
"**Right:** Anti-aliased decimation — applies Butterworth low-pass filter at Nyquist limit "
"before decimation. Frequencies above f_s/2 are cleanly removed."
)
st.plotly_chart(
build_naive_vs_filtered_figure(result, theme=settings["theme"]),
use_container_width=True
)
st.divider()
# === AUDIO PREVIEW ===
st.subheader("🎧 Audio Preview: Original vs Reconstructed")
p1, p2 = st.columns(2)
with p1:
st.caption("Original continuous signal")
st.audio(
audio_bytes_from_signal(result["continuous_signal"], result["display_rate"]),
format="audio/wav"
)
with p2:
st.caption("Sampled and reconstructed signal")
st.audio(
audio_bytes_from_signal(result["reconstructed_signal"], result["display_rate"]),
format="audio/wav"
)
# ══════════════════════════════════════════════════════
# AUDIO UPLOAD MODE
# ══════════════════════════════════════════════════════
else:
st.subheader("🎙️ Audio Upload: Real-Signal Experiment")
uploaded = st.file_uploader("Upload a WAV file", type=["wav"])
if not uploaded:
st.info(
"📂 Upload a WAV file to compare original audio with downsampled versions "
"using **naive** and **anti-aliased** processing."
)
st.stop()
audio_data = read_uploaded_audio(uploaded)
result = run_audio_simulation(audio_data, settings)
# === STATUS METRICS ===
render_status_panel(result)
# === EXPLANATION LAYER ===
with st.expander("📊 Methodology & Analysis", expanded=True):
st.markdown(result["explanation"])
st.divider()
# === WAVEFORMS ===
st.subheader("📈 Waveform Comparison")
col1, col2 = st.columns(2)
with col1:
st.plotly_chart(
build_audio_waveform_figure(
result["original_signal"], result["original_rate"],
f"Original Audio ({result['original_rate']} Hz)", theme=settings["theme"]
),
use_container_width=True
)
with col2:
st.plotly_chart(
build_audio_waveform_figure(
result["processed_signal"], result["original_rate"],
f"Processed Audio — {result['mode_label']}", theme=settings["theme"]
),
use_container_width=True
)
st.divider()
# === SPECTRUM OVERLAY ===
st.subheader("🎵 Spectrum: Original vs Selected Mode")
st.plotly_chart(
build_spectrum_overlay_figure(result, theme=settings["theme"]),
use_container_width=True
)
st.divider()
# === KEY CHART: naive vs filtered for audio ===
st.subheader("⚖️ Comparative Analysis: Naive vs Anti-Aliased (Audio)")
st.caption(
"See how aliasing artifacts appear in the naive downsampled spectrum vs the clean filtered version."
)
st.plotly_chart(
build_audio_naive_vs_filtered_figure(result, theme=settings["theme"]),
use_container_width=True
)
st.divider()
# === AUDIO PLAYBACK ===
st.subheader("🎧 Listen & Compare")
p1, p2 = st.columns(2)
with p1:
st.caption(f"Original audio ({result['original_rate']} Hz)")
st.audio(
audio_bytes_from_signal(result["original_signal"], result["original_rate"]),
format="audio/wav"
)
with p2:
st.caption(
f"Processed audio ({result['target_rate']} Hz target, "
f"replayed at {result['original_rate']} Hz)"
)
st.audio(
audio_bytes_from_signal(result["processed_signal"], result["original_rate"]),
format="audio/wav"
)