119 lines
4.3 KiB
Matlab
119 lines
4.3 KiB
Matlab
function [windowVector, metrics] = create_window_with_metrics(windowType, windowLength, varargin)
|
|
% CREATE_WINDOW_WITH_METRICS - 윈도우 함수 생성 및 성능 지표 계산
|
|
%
|
|
% 입력:
|
|
% windowType - 윈도우 타입 문자열: 'none', 'hann', 'hamming', 'blackman', 'chebwin'
|
|
% windowLength - 윈도우 길이 (샘플 수)
|
|
% varargin - 추가 파라미터 (예: chebwin의 경우 sidelobe level)
|
|
%
|
|
% 출력:
|
|
% windowVector - 생성된 윈도우 벡터 (1 x windowLength)
|
|
% metrics - 윈도우 성능 지표 구조체
|
|
% .type : 윈도우 타입
|
|
% .length : 윈도우 길이
|
|
% .snr_loss_dB : SNR 손실 (dB)
|
|
% .scalloping_loss_dB: Scalloping 손실 (dB)
|
|
% .coherent_gain : 코히어런트 이득
|
|
% .enbw : Equivalent Noise Bandwidth (bins)
|
|
|
|
% 기본 파라미터 설정
|
|
if windowLength <= 0
|
|
error('Window length must be positive.');
|
|
end
|
|
|
|
% 윈도우 함수 생성
|
|
switch lower(windowType)
|
|
case 'none'
|
|
windowVector = ones(1, windowLength);
|
|
|
|
case 'hann'
|
|
windowVector = hann(windowLength)';
|
|
|
|
case 'hamming'
|
|
windowVector = hamming(windowLength)';
|
|
|
|
case 'blackman'
|
|
windowVector = blackman(windowLength)';
|
|
|
|
case 'chebwin'
|
|
% Chebyshev 윈도우는 sidelobe level 파라미터 필요 (기본값: 60dB)
|
|
if ~isempty(varargin)
|
|
sidelobe_dB = varargin{1};
|
|
else
|
|
sidelobe_dB = 60;
|
|
end
|
|
windowVector = chebwin(windowLength, sidelobe_dB)';
|
|
|
|
otherwise
|
|
warning('Unknown window type "%s". Using Hann window as default.', windowType);
|
|
windowVector = hann(windowLength)';
|
|
windowType = 'hann';
|
|
end
|
|
|
|
% 윈도우 성능 지표 계산
|
|
metrics = calculate_window_metrics(windowVector, windowType);
|
|
end
|
|
|
|
|
|
function metrics = calculate_window_metrics(windowVector, windowType)
|
|
% CALCULATE_WINDOW_METRICS - 윈도우 함수의 성능 지표 계산
|
|
%
|
|
% 계산 항목:
|
|
% 1. SNR Loss (dB) : 윈도우 적용으로 인한 SNR 손실
|
|
% 2. Scalloping Loss (dB): FFT bin 사이(0.5 bin offset)에서의 최대 손실
|
|
% 3. Coherent Gain : 윈도우의 평균 진폭
|
|
% 4. ENBW (bins) : Equivalent Noise Bandwidth
|
|
|
|
w = windowVector(:).'; % Row vector로 변환
|
|
N = numel(w);
|
|
|
|
if N == 0
|
|
metrics = struct('type', windowType, 'length', 0, ...
|
|
'snr_loss_dB', NaN, 'scalloping_loss_dB', NaN, ...
|
|
'coherent_gain', NaN, 'enbw', NaN);
|
|
return;
|
|
end
|
|
|
|
% 1. Coherent Gain (코히어런트 이득)
|
|
coherent_gain = mean(w);
|
|
|
|
% 2. Noise Power Gain (잡음 전력 이득)
|
|
noise_power_gain = mean(abs(w).^2);
|
|
|
|
% 3. SNR Loss (dB)
|
|
% SNR_loss = (Noise Power Gain) / (Coherent Gain)^2
|
|
% 이는 윈도우 적용 시 신호 대 잡음비가 얼마나 감소하는지를 나타냄
|
|
if abs(coherent_gain) > eps
|
|
snr_loss_linear = noise_power_gain / (abs(coherent_gain)^2);
|
|
snr_loss_dB = 10 * log10(snr_loss_linear);
|
|
else
|
|
snr_loss_dB = NaN;
|
|
end
|
|
|
|
% 4. Equivalent Noise Bandwidth (ENBW)
|
|
% ENBW는 윈도우가 얼마나 많은 주파수 bin의 잡음을 통과시키는지 나타냄
|
|
enbw = N * noise_power_gain / (sum(w)^2);
|
|
|
|
% 5. Scalloping Loss (dB)
|
|
% FFT bin 중간(0.5 bin offset)에 신호가 위치할 때의 최대 손실
|
|
% 이는 가장 나쁜 경우의 신호 손실을 나타냄
|
|
sample_index = 0:(N-1);
|
|
half_bin_response = abs(sum(w .* exp(-1j * 2 * pi * 0.5 * sample_index / N)));
|
|
dc_response = abs(sum(w));
|
|
|
|
if dc_response > eps
|
|
scalloping_loss_dB = -20 * log10(half_bin_response / dc_response);
|
|
else
|
|
scalloping_loss_dB = NaN;
|
|
end
|
|
|
|
% 결과 구조체 생성
|
|
metrics = struct(...
|
|
'type', lower(windowType), ...
|
|
'length', N, ...
|
|
'snr_loss_dB', snr_loss_dB, ...
|
|
'scalloping_loss_dB', scalloping_loss_dB, ...
|
|
'coherent_gain', coherent_gain, ...
|
|
'enbw', enbw);
|
|
end
|