카테고리
Exercise 03exercise-03
코드와 해설을 함께 읽는 학습 문서
Code Detail
sinusoid and spwm
ex-03/main_spwm.m
코드를 복사해 Octave에서 바로 실행할 수 있습니다.
83 lines
# filename: main_spwm.m
# writer: won sunggyu
# date: 2023-12-08, 2025-03-27
# description: sinusoid and spwm
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);
x = chirp(t, 0, 1, 60); % 0-60Hz
# SPWM
fpwm = 2000;
z = spwm(x, fs, fpwm);
# FFT two-sided
Y = fft(x) / nn;
Z = fft(z) / nn;
Y = abs(Y);
Z = abs(Z);
# FFT Shift
f = f - df * nn / 2;
Y = fftshift(Y); % 1xN
Z = fftshift(Z); % 1xN
# 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_at = {"Xlabel", "Time [sec]", "Ylabel", "Amplitude", "Xlim", [0, 0.1], "Ylim", [-1, 1]};
param_af = {"Xlabel", "Frequency [Hz]", "Ylabel", "Amplitude", "Xlim", [-10000, 10000], "Ylim", [0, 1]};
param_atf = {"Xlabel", "Time [sec]", "Ylabel", "Frequency [Hz]"};
figured(param_f);
axes = subplots(2, 1);
plot(axes(1), t, x, ";Sinusoid;");
plot(axes(1), t, z, ";PWM;");
set(axes(1), param_at{:});
axis tight
plot(axes(2), f, Y, ";Sinusoid;");
plot(axes(2), f, Z, ";PWM;");
set(axes(2), param_af{:});
axis tight
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_plotyy.m
ex-03/main-03.m