main_spwm
sinusoid and spwm
ex-03/main_spwm.m
전체 코드
전체 코드를 복사해서 Octave에서 바로 실행할 수 있습니다.
# 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 코드 해설
목적
sinusoid and spwm
입력
- 스크립트 상단에서 정의한 파라미터/입력 데이터를 사용합니다.
출력
- 그래프/figure 출력
- 콘솔 텍스트 출력
실행 흐름
- Sampling
- FFT two-sided
- FFT Shift
- Visualization
핵심 함수/주제
axesplotabsfiguredsubplotsfffftfftshift
실습 과제
- 샘플링 주파수나 입력 주파수를 바꿔 스펙트럼 변화를 비교해보세요.
- 질량/감쇠/강성 또는 전달함수 계수를 바꿔 응답 변화를 확인해보세요.
- 축 범위와 라벨을 바꿔 그래프 해석성이 어떻게 달라지는지 확인해보세요.
학습 팁
- FFT 결과는 샘플링 주파수(fs)와 길이(nn) 설정에 민감하므로 먼저 축 정의를 확인하세요.
- 그래프 비교 시 축 범위(XLim/YLim)와 단위를 먼저 고정하면 해석 오류를 줄일 수 있습니다.
같은 카테고리의 다른 코드
- main_splecgram
ex-03/main_splecgram.m - main-03
ex-03/main_spwm_plotyy.m - main-03
ex-03/main-03.m