In [14]: import numpy as np
import math
import cmath
import matplotlib.pyplot as plt
from scipy.io import wavfile
# -------- Provide your audio path here --------
audio_path = r"C:\Users\sabar\Downloads\visva audio.wav" # Change this to your fil
# -------- DIT FFT --------
def dit_fft(x):
N = len(x)
x = np.array(x, dtype=complex)
if N <= 1:
return x
even = dit_fft(x[0::2])
odd = dit_fft(x[1::2])
T = [cmath.exp(-2j * math.pi * k / N) * odd[k] for k in range(N // 2)]
return np.array([even[k] + T[k] for k in range(N // 2)] +
[even[k] - T[k] for k in range(N // 2)])
# -------- DIF FFT --------
def dif_fft(x):
N = len(x)
x = np.array(x, dtype=complex)
if N <= 1:
return x
half = N // 2
X = np.zeros(N, dtype=complex)
for k in range(half):
a = x[k]
b = x[k + half]
X[k] = a + b
X[k + half] = (a - b) * cmath.exp(-2j * math.pi * k / N)
left = dif_fft(X[:half])
right = dif_fft(X[half:])
return np.concatenate([left, right])
# -------- Bit-reversal reorder --------
def bit_reverse_copy(x):
N = len(x)
result = np.zeros(N, dtype=complex)
bits = int(np.log2(N))
for i in range(N):
rev = int('{:0{width}b}'.format(i, width=bits)[::-1], 2)
result[rev] = x[i]
return result
# -------- Load audio file --------
fs, audio = wavfile.read(audio_path)
# If stereo, take one channel
if audio.ndim > 1:
audio = audio[:, 0]
# Take N samples (power of 2)
N = 1024
audio = audio[:N].astype(float)
# -------- Compute DIT & DIF for original --------
file:///C:/Users/sabar/Downloads/LAB4.html 1/3
audio_dit = dit_fft(audio)
audio_dif = bit_reverse_copy(dif_fft(audio)) # reorder so it matches DIT
# -------- Create 25000 Hz sine --------
f_sine = 25000 # Hz
t = np.arange(N) / fs
sine_wave = 0.5 * np.sin(2 * np.pi * f_sine * t)
# -------- (DIT of Original) + (Sine wave) --------
dit_plus_sine = np.abs(audio_dit) + sine_wave
# -------- DIT of (Original + Sine wave) --------
audio_plus_sine = audio + sine_wave
dit_of_mixed = dit_fft(audio_plus_sine)
# -------- Frequency axis --------
freqs = np.fft.fftfreq(N, d=1/fs)
# -------- Plot in 3x2 layout --------
fig, axes = plt.subplots(3, 2, figsize=(12, 10))
# 1. Original Audio
axes[0, 0].plot(t, audio)
axes[0, 0].set_title("Original Audio (Time Domain)")
axes[0, 0].set_xlabel("Time (s)")
axes[0, 0].set_ylabel("Amplitude")
# 2. DIT of Original
axes[0, 1].plot(freqs[:N//2], np.abs(audio_dit[:N//2]))
axes[0, 1].set_title("DIT FFT of Original")
axes[0, 1].set_xlabel("Frequency (Hz)")
axes[0, 1].set_ylabel("Magnitude")
# 3. DIF of Original
axes[1, 0].plot(freqs[:N//2], np.abs(audio_dif[:N//2]))
axes[1, 0].set_title("DIF FFT of Original")
axes[1, 0].set_xlabel("Frequency (Hz)")
axes[1, 0].set_ylabel("Magnitude")
# 4. (DIT of Original) + (Sine wave 25000 Hz)
axes[1, 1].plot(freqs[:N//2], dit_plus_sine[:N//2])
axes[1, 1].set_title("(DIT of Original) + (25000 Hz Sine)")
axes[1, 1].set_xlabel("Frequency (Hz)")
axes[1, 1].set_ylabel("Magnitude")
# 5. DIT of (Original + 25000 Hz Sine)
axes[2, 0].plot(freqs[:N//2], np.abs(dit_of_mixed[:N//2]))
axes[2, 0].set_title("DIT FFT of (Original + 25000 Hz Sine)")
axes[2, 0].set_xlabel("Frequency (Hz)")
axes[2, 0].set_ylabel("Magnitude")
# Hide unused subplot (bottom right)
axes[2, 1].axis("off")
plt.tight_layout()
plt.show()
file:///C:/Users/sabar/Downloads/LAB4.html 2/3
In [ ]:
file:///C:/Users/sabar/Downloads/LAB4.html 3/3