카테고리
Exercise 03exercise-03
코드와 해설을 함께 읽는 학습 문서
Code Detail
sinusoid signal and auto-power spectrum
ex-03/main_spwm_plotyy.m
코드를 복사해 Octave에서 바로 실행할 수 있습니다.
86 lines
# filename: main-03.m
# writer: won sunggyu
# date: 2025-03-26
# description: sinusoid signal and auto-power spectrum
run("./startup.m");
addpath(genpath("./mylib"));
printf(fmt("{mfilename}\n", "#FF5733"));
# Sampling
[fs, duration] = deal(20000, 3);
[fs, duration, dt, df, nn] = sampling_settings(fs, duration);
[t, f] = make_axes(fs, duration);
# Sinusoid
frequency = [60];
phase = [0];
amplitude = [1];
[t, x] = generate_sinusoid(frequency, phase, amplitude, fs, duration);
# SPWM
z = spwm(x);
# FFT
Y = fft(x) / nn;
Z = fft(z) / nn;
Y = abs(Y);
Z = abs(Z);
# Spectrogram
over_r = 0.9;
n_wind = 1280;
n_ov_r = floor(n_wind * over_r);
hann_w = hanning(n_wind);
[S, ff, tt] = specgram(x, n_wind, fs, hann_w, n_ov_r);
frng = 2:n_wind*4000/fs;
S = abs(S(frng,:)); # magnitude in range 0<f<=4000 Hz.
S = S / max(S(:)); # normalize magnitude so that max is 0 dB.
# Visualization
param_f = {"Size", [960, 960], "Move", [-1280, 0], "Name", "Sinusoid"};
param_atf = {"Xlabel", "Time [sec]", "Ylabel", "Frequency [Hz]"};
param_at1 = {"Xlabel", "Time [sec]", "Ylabel", "x", "Xlim", [0, 0.01], "Ylim", [-1, 1]};
param_at2 = {"Xlabel", "Time [sec]", "Ylabel", "z", "Xlim", [0, 0.01], "Ylim", [-1, 1]};
param_af1 = {"Xlabel", "Frequency [Hz]", "Ylabel", "X", "Xlim", [-500, 500], "Ylim", [0, 1]};
param_af2 = {"Xlabel", "Frequency [Hz]", "Ylabel", "Z", "Xlim", [-500, 500], "Ylim", [0, 1]};
figured(param_f);
axes = subplots(2, 1);
[ax, h1, h2] = plotyy(axes(1), t, x, t, z);
textd(axes(1), 0.05, 0.90, "Sinusoid");
textd(axes(1), 0.05, 0.80, "PWM");
set(ax(1), param_at1{:});
set(ax(2), param_at2{:});
# FFT Shift
f = f - df * nn / 2;
Y = fftshift(Y); % 1xN
Z = fftshift(Z); % 1xN
[ax, h1, h2] = plotyy(axes(2), f, Y, f, Z);
textd(axes(2), 0.05, 0.90, "FFT of Sinusoid");
textd(axes(2), 0.05, 0.80, "FFT of PWM");
set(ax(1), param_af1{:});
set(ax(2), param_af2{:});
return
# Visualization
figured(param_f);
subplots(param_atf);
imagesc (tt, ff(frng), log(S));
axis tight
colorbar
# Visualization
figured(param_f);
subplots(param_atf);
waterfall(tt, ff(frng), log(S));
axis tight
colorbar ex-03/main_splecgram.m
ex-03/main_spwm.m
ex-03/main-03.m