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