File size: 16,696 Bytes
1835c00
 
 
 
 
 
 
 
 
 
 
 
 
 
6f56f73
1835c00
6f56f73
 
 
1835c00
 
 
6f56f73
1835c00
 
6f56f73
 
 
 
 
 
 
 
 
1835c00
 
6f56f73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1835c00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f56f73
 
1835c00
 
6f56f73
 
 
1835c00
 
 
 
 
 
 
6f56f73
 
 
 
 
 
1835c00
 
 
 
 
 
 
 
 
 
 
6f56f73
1835c00
6f56f73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1835c00
 
6f56f73
 
 
 
 
1835c00
6f56f73
1835c00
6f56f73
1835c00
6f56f73
 
 
 
 
1835c00
 
 
 
 
 
 
 
 
 
6f56f73
 
 
 
 
1835c00
 
6f56f73
 
 
 
 
 
 
 
 
 
1835c00
6f56f73
1835c00
6f56f73
 
 
 
 
 
 
 
 
 
 
1835c00
 
6f56f73
1835c00
 
 
 
6f56f73
 
 
 
 
 
 
 
 
 
1835c00
6f56f73
 
 
 
 
 
 
1835c00
6f56f73
 
 
 
 
 
 
 
 
 
 
1835c00
6f56f73
 
 
 
 
 
 
 
1835c00
 
6f56f73
1835c00
6f56f73
 
 
 
 
 
1835c00
6f56f73
 
 
 
 
 
 
1835c00
 
6f56f73
1835c00
6f56f73
 
 
 
 
 
 
 
1835c00
 
 
6f56f73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1835c00
6f56f73
 
 
 
 
 
 
1835c00
6f56f73
 
 
 
 
 
 
1835c00
6f56f73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1835c00
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import numpy as np
import librosa
import librosa.display
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
import matplotlib.pyplot as plt
import plotly.express as px
import soundfile as sf
from scipy.signal import stft
import math

# -------------------------------
# CNN Model for Audio Analysis
# -------------------------------
class AudioCNN(nn.Module):
    def __init__(self):
        super(AudioCNN, self).__init__()
        # Convolutional layers
        self.conv1 = nn.Conv2d(1, 16, kernel_size=3, padding=1)
        self.conv2 = nn.Conv2d(16, 32, kernel_size=3, padding=1)
        self.conv3 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
        # Pooling layer
        self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
        # Fully connected layers (with dynamic sizing)
        self.fc1 = None
        self.fc2 = nn.Linear(256, 128)
        self.fc3 = nn.Linear(128, 10)
        # Dropout for regularization
        self.dropout = nn.Dropout(0.5)

    def forward(self, x):
        x1 = F.relu(self.conv1(x))
        x2 = self.pool(x1)
        x3 = F.relu(self.conv2(x2))
        x4 = self.pool(x3)
        x5 = F.relu(self.conv3(x4))
        x6 = self.pool(x5)
        if self.fc1 is None:
            fc1_input_size = x6.numel() // x6.size(0)
            self.fc1 = nn.Linear(fc1_input_size, 256)
        x7 = x6.view(x6.size(0), -1)
        x8 = F.relu(self.fc1(x7))
        x9 = self.dropout(x8)
        x10 = F.relu(self.fc2(x9))
        x11 = self.fc3(x10)
        return x11, [x2, x4, x6], x8

# -------------------------------
# Audio Processing Functions
# -------------------------------
def load_audio(file):
    audio, sr = librosa.load(file, sr=None, mono=True)
    return audio, sr

def apply_fft(audio):
    fft = np.fft.fft(audio)
    magnitude = np.abs(fft)
    phase = np.angle(fft)
    return fft, magnitude, phase

def filter_fft(fft, percentage):
    magnitude = np.abs(fft)
    sorted_indices = np.argsort(magnitude)[::-1]
    num_keep = int(len(sorted_indices) * percentage / 100)
    mask = np.zeros_like(fft)
    mask[sorted_indices[:num_keep]] = 1
    return fft * mask

def create_spectrogram(audio, sr):
    n_fft = 2048
    hop_length = 512
    S = librosa.stft(audio, n_fft=n_fft, hop_length=hop_length)
    spectrogram = np.abs(S)
    return spectrogram, n_fft, hop_length

# -------------------------------
# Visualization Functions
# -------------------------------
def plot_waveform(audio, sr, title):
    fig = go.Figure()
    time = np.arange(len(audio)) / sr
    fig.add_trace(go.Scatter(x=time, y=audio, mode='lines'))
    fig.update_layout(title=title, xaxis_title='Time (s)', yaxis_title='Amplitude')
    return fig

def create_waveform_table(audio, sr, num_samples=100):
    time = np.arange(len(audio)) / sr
    indices = np.linspace(0, len(audio)-1, num_samples, dtype=int)
    df = pd.DataFrame({"Time (s)": time[indices], "Amplitude": audio[indices]})
    return df

def plot_fft(magnitude, phase, sr):
    fig = make_subplots(rows=2, cols=1, subplot_titles=('Magnitude Spectrum', 'Phase Spectrum'))
    freq = np.fft.fftfreq(len(magnitude), 1/sr)
    fig.add_trace(go.Scatter(x=freq, y=magnitude, mode='lines', name='Magnitude'), row=1, col=1)
    fig.add_trace(go.Scatter(x=freq, y=phase, mode='lines', name='Phase'), row=2, col=1)
    fig.update_xaxes(title_text='Frequency (Hz)', row=1, col=1)
    fig.update_xaxes(title_text='Frequency (Hz)', row=2, col=1)
    fig.update_yaxes(title_text='Magnitude', row=1, col=1)
    fig.update_yaxes(title_text='Phase (radians)', row=2, col=1)
    return fig

def plot_fft_bands(magnitude, phase, sr):
    freq = np.fft.fftfreq(len(magnitude), 1/sr)
    pos_mask = freq >= 0
    freq, magnitude, phase = freq[pos_mask], magnitude[pos_mask], phase[pos_mask]
    bass_mask = (freq >= 20) & (freq < 250)
    mid_mask = (freq >= 250) & (freq < 4000)
    treble_mask = (freq >= 4000) & (freq <= sr/2)
    fig = make_subplots(rows=2, cols=1, subplot_titles=('Magnitude Spectrum by Bands', 'Phase Spectrum by Bands'))
    fig.add_trace(go.Scatter(x=freq[bass_mask], y=magnitude[bass_mask], mode='lines', name='Bass'), row=1, col=1)
    fig.add_trace(go.Scatter(x=freq[mid_mask], y=magnitude[mid_mask], mode='lines', name='Mid'), row=1, col=1)
    fig.add_trace(go.Scatter(x=freq[treble_mask], y=magnitude[treble_mask], mode='lines', name='Treble'), row=1, col=1)
    fig.add_trace(go.Scatter(x=freq[bass_mask], y=phase[bass_mask], mode='lines', name='Bass'), row=2, col=1)
    fig.add_trace(go.Scatter(x=freq[mid_mask], y=phase[mid_mask], mode='lines', name='Mid'), row=2, col=1)
    fig.add_trace(go.Scatter(x=freq[treble_mask], y=phase[treble_mask], mode='lines', name='Treble'), row=2, col=1)
    fig.update_xaxes(title_text='Frequency (Hz)', row=1, col=1)
    fig.update_xaxes(title_text='Frequency (Hz)', row=2, col=1)
    fig.update_yaxes(title_text='Magnitude', row=1, col=1)
    fig.update_yaxes(title_text='Phase (radians)', row=2, col=1)
    return fig

def create_fft_table(magnitude, phase, sr, num_samples=100):
    freq = np.fft.fftfreq(len(magnitude), 1/sr)
    pos_mask = freq >= 0
    freq, magnitude, phase = freq[pos_mask], magnitude[pos_mask], phase[pos_mask]
    indices = np.linspace(0, len(freq)-1, num_samples, dtype=int)
    df = pd.DataFrame({
        "Frequency (Hz)": freq[indices],
        "Magnitude": magnitude[indices],
        "Phase (radians)": phase[indices]
    })
    return df

def plot_3d_polar_fft(magnitude, phase, sr):
    # Get positive frequencies
    freq = np.fft.fftfreq(len(magnitude), 1/sr)
    pos_mask = freq >= 0
    freq, mag, ph = freq[pos_mask], magnitude[pos_mask], phase[pos_mask]
    # Convert polar to Cartesian coordinates
    x = mag * np.cos(ph)
    y = mag * np.sin(ph)
    z = freq  # Use frequency as z-axis

    # Downsample the data to avoid huge message sizes.
    # Compute a decimation factor so that approximately 500 points are plotted.
    step = max(1, len(x) // 500)
    x, y, z, ph = x[::step], y[::step], z[::step], ph[::step]

    # Create a coarser grid for the contour surface.
    n_rep = 10
    X_surface = np.tile(x, (n_rep, 1))
    Y_surface = np.tile(y, (n_rep, 1))
    Z_surface = np.tile(z, (n_rep, 1))
    
    surface = go.Surface(
        x=X_surface,
        y=Y_surface,
        z=Z_surface,
        colorscale='Viridis',
        opacity=0.6,
        showscale=False,
        contours={
            "x": {"show": True, "start": float(np.min(x)), "end": float(np.max(x)), "size": float((np.max(x)-np.min(x))/10)},
            "y": {"show": True, "start": float(np.min(y)), "end": float(np.max(y)), "size": float((np.max(y)-np.min(y))/10)},
            "z": {"show": True, "start": float(np.min(z)), "end": float(np.max(z)), "size": float((np.max(z)-np.min(z))/10)},
        },
    )
    
    scatter = go.Scatter3d(
        x=x,
        y=y,
        z=z,
        mode='markers',
        marker=dict(
            size=3,
            color=ph,  # color by phase
            colorscale='Viridis',
            opacity=0.8,
            colorbar=dict(title='Phase (radians)')
        )
    )
    
    fig = go.Figure(data=[surface, scatter])
    fig.update_layout(scene=dict(
        xaxis_title='Real Component',
        yaxis_title='Imaginary Component',
        zaxis_title='Frequency (Hz)',
        camera=dict(eye=dict(x=1.5, y=1.5, z=0.5))
    ), margin=dict(l=0, r=0, b=0, t=0))
    return fig

def plot_spectrogram(spectrogram, sr, hop_length):
    fig, ax = plt.subplots()
    img = librosa.display.specshow(librosa.amplitude_to_db(spectrogram, ref=np.max),
                                   sr=sr, hop_length=hop_length, x_axis='time', y_axis='log', ax=ax)
    plt.colorbar(img, ax=ax, format='%+2.0f dB')
    plt.title('Spectrogram')
    return fig

def create_spectrogram_table(spectrogram, num_rows=10, num_cols=10):
    sub_spec = spectrogram[:num_rows, :num_cols]
    df = pd.DataFrame(sub_spec, 
                      index=[f'Freq Bin {i}' for i in range(sub_spec.shape[0])],
                      columns=[f'Time Bin {j}' for j in range(sub_spec.shape[1])])
    return df

def create_activation_table(activation, num_rows=10, num_cols=10):
    sub_act = activation[:num_rows, :num_cols]
    df = pd.DataFrame(sub_act, 
                      index=[f'Row {i}' for i in range(sub_act.shape[0])],
                      columns=[f'Col {j}' for j in range(sub_act.shape[1])])
    return df

# -------------------------------
# Streamlit UI & Main App
# -------------------------------
st.set_page_config(layout="wide")
st.title("Audio Frequency Analysis with CNN and FFT")

st.markdown("""
    ### Welcome to the Audio Frequency Analysis Tool!
    This application allows you to:
    - **Upload an audio file** and visualize its waveform along with a data table.
    - **Analyze frequency components** using FFT (with both 2D and enhanced 3D polar plots).
    - **Highlight frequency bands:** Bass (20–250 Hz), Mid (250–4000 Hz), Treble (4000 Hz to Nyquist).
    - **Filter frequency components** and reconstruct the waveform.
    - **Generate a spectrogram** for time-frequency analysis with a sample data table.
    - **Inspect CNN activations** (pooling and dense layers) arranged in grid layouts.
    - **Final Audio Classification:** Classify the audio for gender (Male/Female) and tone.
""")

# File uploader
uploaded_file = st.file_uploader("Upload an audio file (WAV, MP3, OGG)", type=['wav', 'mp3', 'ogg'])

if uploaded_file is not None:
    audio, sr = load_audio(uploaded_file)
    
    # --- Section 1: Raw Audio Waveform ---
    st.header("1. Raw Audio Waveform")
    st.markdown("""
        The waveform represents the amplitude over time.
        **Graph:** Amplitude vs. Time.
        **Data Table:** Sampled values.
    """)
    waveform_fig = plot_waveform(audio, sr, "Original Waveform")
    st.plotly_chart(waveform_fig, use_container_width=True)
    st.dataframe(create_waveform_table(audio, sr))
    
    # --- Section 2: Frequency Domain Analysis ---
    st.header("2. Frequency Domain Analysis")
    st.markdown("""
        **FFT Analysis:** Decompose the audio into frequency components.
        - **Magnitude Spectrum:** Strength of frequencies.
        - **Phase Spectrum:** Phase angles.
    """)
    fft, magnitude, phase = apply_fft(audio)
    col1, col2 = st.columns(2)
    with col1:
        st.subheader("2D FFT Plot")
        st.plotly_chart(plot_fft(magnitude, phase, sr), use_container_width=True)
    with col2:
        st.subheader("Enhanced 3D Polar FFT Plot with Contours")
        st.plotly_chart(plot_3d_polar_fft(magnitude, phase, sr), use_container_width=True)
    st.subheader("FFT Data Table (Sampled)")
    st.dataframe(create_fft_table(magnitude, phase, sr))
    st.subheader("Frequency Bands: Bass, Mid, Treble")
    st.plotly_chart(plot_fft_bands(magnitude, phase, sr), use_container_width=True)
    
    # --- Section 3: Frequency Filtering ---
    st.header("3. Frequency Filtering")
    st.markdown("""
        Filter the audio signal by retaining a percentage of the strongest frequencies.
        Adjust the slider for retention percentage.
        **Graph:** Filtered waveform.
        **Data Table:** Sampled values.
    """)
    percentage = st.slider("Percentage of frequencies to retain:", 0.1, 100.0, 10.0, 0.1)
    if st.button("Apply Frequency Filter"):
        filtered_fft = filter_fft(fft, percentage)
        reconstructed = np.fft.ifft(filtered_fft).real
        col1, col2 = st.columns(2)
        with col1:
            st.plotly_chart(plot_waveform(reconstructed, sr, "Filtered Waveform"), use_container_width=True)
        with col2:
            st.audio(reconstructed, sample_rate=sr)
        st.dataframe(create_waveform_table(reconstructed, sr))
    
    # --- Section 4: Spectrogram Analysis ---
    st.header("4. Spectrogram Analysis")
    st.markdown("""
        A spectrogram shows how frequency content evolves over time.
        **Graph:** Spectrogram (log-frequency scale).
        **Data Table:** A subsection of the spectrogram matrix.
    """)
    spectrogram, n_fft, hop_length = create_spectrogram(audio, sr)
    st.pyplot(plot_spectrogram(spectrogram, sr, hop_length))
    st.dataframe(create_spectrogram_table(spectrogram))
    
    # --- Section 5: CNN Analysis (Pooling & Dense Activations) ---
    st.header("5. CNN Analysis: Pooling and Dense Activations")
    st.markdown("""
        Instead of classification probabilities, inspect internal activations:
        - **Pooling Layer Outputs:** Arranged in a grid layout.
        - **Dense Layer Activation:** Feature vector from the dense layer.
    """)
    if st.button("Run CNN Analysis"):
        spec_tensor = torch.tensor(spectrogram[np.newaxis, np.newaxis, ...], dtype=torch.float32)
        model = AudioCNN()
        with torch.no_grad():
            output, pooling_outputs, dense_activation = model(spec_tensor)
        for idx, activation in enumerate(pooling_outputs):
            st.subheader(f"Pooling Layer {idx+1} Output")
            act = activation[0].cpu().numpy()
            num_channels = act.shape[0]
            ncols = 4
            nrows = math.ceil(num_channels / ncols)
            fig, axes = plt.subplots(nrows, ncols, figsize=(3*ncols, 3*nrows))
            axes = axes.flatten()
            for i in range(nrows * ncols):
                if i < num_channels:
                    axes[i].imshow(act[i], aspect='auto', origin='lower', cmap='viridis')
                    axes[i].set_title(f'Channel {i+1}', fontsize=8)
                    axes[i].axis('off')
                else:
                    axes[i].axis('off')
            st.pyplot(fig)
            st.markdown("**Data Table for Pooling Layer Activation (Channel 1, Sampled)**")
            df_act = create_activation_table(act[0])
            st.dataframe(df_act)
        st.subheader("Dense Layer Activation")
        dense_act = dense_activation[0].cpu().numpy()
        df_dense = pd.DataFrame({
            "Feature Index": np.arange(len(dense_act)),
            "Activation Value": dense_act
        })
        st.plotly_chart(px.bar(df_dense, x="Feature Index", y="Activation Value"), use_container_width=True)
        st.dataframe(df_dense)
    
    # --- Section 6: Final Audio Classification (Gender & Tone) ---
    st.header("6. Final Audio Classification: Gender and Tone")
    st.markdown("""
        In this final step, a pretrained model classifies the audio as Male or Female,
        and determines its tone (High Tone vs. Low Tone).
        
        **Note:** This example uses a placeholder model. Replace the dummy model and random outputs with your actual pretrained model.
    """)
    if st.button("Run Final Classification"):
        # Extract MFCC features as an example (adjust as needed)
        mfccs = librosa.feature.mfcc(y=audio, sr=sr, n_mfcc=40)
        features = np.mean(mfccs, axis=1)  # average over time
        features_tensor = torch.tensor(features, dtype=torch.float32).unsqueeze(0)
        
        # Dummy classifier model for demonstration
        class GenderToneClassifier(nn.Module):
            def __init__(self):
                super(GenderToneClassifier, self).__init__()
                self.fc = nn.Linear(40, 4)  # 4 outputs: [Male, Female, High Tone, Low Tone]
            def forward(self, x):
                return self.fc(x)
        
        classifier = GenderToneClassifier()
        # In practice, load your pretrained weights here.
        with torch.no_grad():
            output = classifier(features_tensor)
            probs = F.softmax(output, dim=1).numpy()[0]
        # Interpret outputs: assume first 2 are gender, next 2 are tone.
        gender = "Male" if probs[0] > probs[1] else "Female"
        tone = "High Tone" if probs[2] > probs[3] else "Low Tone"
        st.markdown(f"**Predicted Gender:** {gender}")
        st.markdown(f"**Predicted Tone:** {tone}")
        categories = ["Male", "Female", "High Tone", "Low Tone"]
        df_class = pd.DataFrame({"Category": categories, "Probability": probs})
        st.plotly_chart(px.bar(df_class, x="Category", y="Probability"), use_container_width=True)
        st.dataframe(df_class)

# -------------------------------
# Style Enhancements
# -------------------------------
st.markdown("""
    <style>
        .stButton>button {
            padding: 10px 20px;
            font-size: 16px;
            background-color: #4CAF50;
            color: white;
        }
        .stSlider>div>div>div>div {
            background-color: #4CAF50;
        }
    </style>
""", unsafe_allow_html=True)