-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from kuu8902/kkoiso/chapter02
Add chapter02
- Loading branch information
Showing
10 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import numpy as np | ||
def DFT(x): | ||
N = len(x) | ||
X = [0] * N | ||
for k in range(N): | ||
s=0 | ||
for n in range(N): | ||
a = ((-2j) * np.pi * k * n) / N | ||
s = s + x[n] * np.exp(a) | ||
|
||
X[k] = s | ||
return X | ||
|
||
def IDFT(X): | ||
N = len(X) | ||
x = [0] * N | ||
for n in range(N): | ||
s=0 | ||
for k in range(N): | ||
angle = (2j * np.pi * k * n) / N | ||
s = s + X[k] * np.exp(angle) | ||
x[n] =s/N | ||
return x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import numpy as np | ||
from q01 import DFT | ||
|
||
x = np.array([1, 0, 0, 0, 0, 0, 0, 0]) | ||
X = DFT(x) | ||
|
||
|
||
print(X) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from q01 import DFT,IDFT | ||
|
||
x = np.array([1, 0, 0, 0, 0, 0, 0, 0]) | ||
X = DFT(x) | ||
x_idft = IDFT(X) | ||
print(x_idft) | ||
idft_real = [val.real for val in x_idft]#実部を取りだす | ||
idft_imag = [val.imag for val in x_idft]#虚部を取り出す | ||
|
||
#実部をプロット | ||
plt.figure(figsize=(12, 6)) | ||
plt.subplot(2, 1, 1) | ||
plt.stem(idft_real) | ||
plt.title('IDFT(Real Part)') | ||
plt.xlabel('Sample') | ||
plt.ylabel('Amplitude') | ||
|
||
#虚部をプロット | ||
plt.subplot(2, 1, 2) | ||
plt.stem(idft_imag) | ||
plt.title('IDFT Result (Imaginary Part)') | ||
plt.xlabel('Sample') | ||
plt.ylabel('Amplitude') | ||
|
||
plt.tight_layout() | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from q01 import DFT | ||
|
||
x = np.array([1, 0, 0, 0, 0, 0, 0, 0]) | ||
X = DFT(x) | ||
title = "DFT impulse" | ||
|
||
plt.figure(figsize=(10, 4)) | ||
|
||
#振幅スペクトルのプロット | ||
plt.subplot(2, 1, 1) | ||
plt.plot(np.abs(X)) | ||
plt.title(title + ' Amplitude Spectrum') | ||
plt.xlabel('Frequency') | ||
plt.ylabel('Amplitude') | ||
plt.grid(True) | ||
|
||
#位相スペクトルのプロット | ||
plt.subplot(2, 1, 2) | ||
plt.plot(np.angle(X)) | ||
plt.title(title + ' Phase Spectrum') | ||
plt.xlabel('Frequency') | ||
plt.ylabel('Phase') | ||
plt.grid(True) | ||
|
||
plt.tight_layout() | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from q01 import DFT | ||
|
||
x = np.array([1, 0, 0, 0, 0, 0, 0, 0]) | ||
X = DFT(x) | ||
|
||
|
||
np_X = np.fft.fft(x) | ||
print("q02 DFT:", X) | ||
print("Np FFT:", np_X) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
|
||
a = 1.0 | ||
freq= 440 | ||
samp_rate = 16000 | ||
d = 3 | ||
|
||
t = np.linspace(0, d, int(samp_rate * d), endpoint=False) | ||
|
||
|
||
sin_wave = a * np.sin(2 * np.pi * freq * t) | ||
|
||
X = np.fft.fft(sin_wave) | ||
|
||
#振幅をデシベル表記 | ||
amplitude_spectrum = 20 * np.log10(np.abs(X)) | ||
|
||
# 位相スペクトルの計算 | ||
phase_spectrum = np.angle(X) | ||
|
||
#小さい振幅の位相も取っていたため、意味不明な位相スペクトルが出ていたので修正 | ||
for i in range(len(phase_spectrum)): | ||
|
||
if amplitude_spectrum[i] < 0.001: | ||
phase_spectrum[i] = 0 | ||
|
||
|
||
|
||
# 対応する周波数を計算 | ||
frequencies = np.fft.fftfreq(len(X), 1/samp_rate) | ||
|
||
|
||
plt.figure(figsize=(12, 6)) | ||
# 振幅スペクトルのプロット | ||
plt.subplot(2, 1, 1) | ||
plt.plot(frequencies,amplitude_spectrum) | ||
plt.title('Amplitude Spectrum') | ||
plt.xlabel('Frequency (Hz)') | ||
plt.ylabel('Amplitude (dB)') | ||
plt.grid(True) | ||
|
||
# 位相スペクトルのプロット | ||
plt.subplot(2, 1, 2) | ||
plt.plot(frequencies, phase_spectrum) | ||
plt.title('Phase Spectrum') | ||
plt.xlabel('Frequency (Hz)') | ||
plt.ylabel('Phase (radians)') | ||
plt.grid(True) | ||
|
||
plt.tight_layout() | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
def Hamming_Window(N): | ||
n = np.arange(N) | ||
return 0.54 - 0.46 * np.cos((2 * np.pi * n )/ (N - 1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from q07 import Hamming_Window | ||
#正弦波を生成 | ||
a = 1.0 | ||
freq= 440 | ||
samp_rate = 16000 | ||
d = 3 | ||
|
||
t = np.linspace(0, d, int(samp_rate * d), endpoint=False) | ||
|
||
|
||
sin_wave = a * np.sin(2 * np.pi * freq * t) | ||
#ハミング窓を生成し、正弦波にかける | ||
signal_length = len(sin_wave) | ||
hamming_window_signal = sin_wave * Hamming_Window(signal_length) | ||
|
||
#プロット | ||
plt.figure(figsize=(10, 4)) | ||
plt.plot(hamming_window_signal) | ||
plt.title('Signal with Hamming Window') | ||
plt.xlabel('Sample') | ||
plt.ylabel('Amplitude') | ||
plt.grid(True) | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from q07 import Hamming_Window | ||
#正弦波を生成 | ||
a = 1.0 | ||
freq= 440 | ||
samp_rate = 16000 | ||
d = 3 | ||
|
||
t = np.linspace(0, d, int(samp_rate * d), endpoint=False) | ||
|
||
|
||
sin_wave = a * np.sin(2 * np.pi * freq * t) | ||
|
||
signal_length = len(sin_wave) | ||
#正弦波と同じサイズのハミング窓を生成し、DFT | ||
hamming_window_spectrum = np.fft.fft(Hamming_Window(signal_length)) | ||
|
||
print(hamming_window_spectrum) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from q07 import Hamming_Window | ||
from q01 import DFT,IDFT | ||
#正弦波を生成 | ||
a = 1.0 | ||
freq= 440 | ||
samp_rate = 16000 | ||
d = 3 | ||
|
||
t = np.linspace(0, d, int(samp_rate * d), endpoint=False) | ||
|
||
|
||
sin_wave = a * np.sin(2 * np.pi * freq * t) | ||
#正弦波をDFT | ||
sin_wave_DFT = np.fft.fft(sin_wave) | ||
|
||
|
||
#正弦波と同じサイズのハミング窓を生成し、DFT | ||
signal_length = len(sin_wave) | ||
hamming_window_spectrum = np.fft.fft(Hamming_Window(signal_length)) | ||
|
||
#巡回畳み込み | ||
Z=[0] * signal_length | ||
for k in range(signal_length): | ||
s = 0 | ||
for n in range(signal_length): | ||
s =s +sin_wave_DFT[n]*hamming_window_spectrum[k-n] | ||
Z[k] = s | ||
z_idft = np.fft.ifft(Z) | ||
print(z_idft) | ||
|
||
|
||
#プロット | ||
plt.figure(figsize=(10, 4)) | ||
plt.plot(z_idft) | ||
plt.title('Signal with Hamming Window') | ||
plt.xlabel('Sample') | ||
plt.ylabel('Amplitude') | ||
plt.grid(True) | ||
plt.show() |