When I adjusted the hanning window's 'symmetric' and 'periodic' parameters I got very different SNR results.

3 次查看(过去 30 天)
When I calculate sinusoidal signal to noise ratio, I add the hanning window. But when I adjusted the hanning window's 'symmetric' and 'periodic' parameters I got very different results.
When I set default or 'symmetric', SNR of the signal will be far less than when I set 'periodic'
Result of 'symmetric':
SNR of 1 signal: 126.739348
SNR of 2 signal: 114.118917
SNR of 3 signal: 102.379880
Result of 'periodic':
SNR of 1 signal: 270.135232
SNR of 2 signal: 114.363203
SNR of 3 signal: 114.463044
But the quantized signal will not be affected.
Futhermore, the length of signal will also affect SNR results? Why this wwll happen? Here are my code:
clear;
clc;
fig_num = 0;
% Generate sin signal.
Fs = 192e3; % Sample frequency.(Hz)
OSR = 16; % Oversample ratio.
Fs_OSR = Fs * OSR;
F0 = 1e3; % Sin signal frequency.(Hz)
Amp = 0.9; % Sin signal amplitude.
duration = 3e-1; % Sin signal duration time.(s)
N_OSR = duration*Fs_OSR; % Sin signal sample number.
t_OSR = (0:1/Fs_OSR:(N_OSR-1)/Fs_OSR)'; % Sin signal sample points.
signal_OSR = Amp * sin(2*pi*F0*t_OSR); % Generate sin signal.
fig_num = fig_num + 1;
plot_signal(signal_OSR, Fs_OSR, F0, fig_num)
% Define bit width and fractional bits
bit_width = 16; % Total number of bits
frac_bits = bit_width - 1; % Number of fractional bits
% Generate quantized signal.
mode = 'fixed';
round_mode = 'ceil';
overflow_mode = 'saturate';
format = [bit_width, frac_bits];
q_obj = quantizer(mode, round_mode, overflow_mode, format);
signal_OSR_quantized = quantize(q_obj, signal_OSR);
fig_num = fig_num + 1;
plot_signal(signal_OSR_quantized, Fs_OSR, F0, fig_num)
% Generate CIC signal.
cicDecim = dsp.CICDecimator('DecimationFactor', OSR);
signal_cicDecim_quantized = cicDecim(signal_OSR_quantized);
scale_factor = max(signal_OSR_quantized)/max(signal_cicDecim_quantized);
signal_cicDecim_quantized = signal_cicDecim_quantized * scale_factor;
fig_num = fig_num + 1;
plot_signal(signal_cicDecim_quantized, Fs, F0, fig_num)
Fin_bin = F0 * length(signal_cicDecim_quantized) / Fs;
function plot_signal(signal, Fs, F0, fig_num)
N = length(signal);
f0_normalization = F0 * N / Fs;
f_x_axis = Fs*(0:fix(N/2)-1)/N;
window = hanning(N, 'periodic');
signal = signal .* window;
signal_spectrum_hanning = abs(fft(signal)/N) * 2;
signal_spectrum_hanning = signal_spectrum_hanning(1:fix(N/2));
near_point = 1;
f_cut = 20e3;
cut_index = f_cut * N / Fs;
% cut_index = fix(N/2);
all_index = 1 + (0:cut_index-1);
signal_index = 1 + (f0_normalization + (-near_point:near_point));
DC_index = 1 + (0:near_point);
noise_index = setdiff(setdiff(all_index, signal_index),DC_index);
SNR = 10*log10(sum(signal_spectrum_hanning(signal_index).^2) / ...
sum(signal_spectrum_hanning(noise_index).^2));
fprintf('SNR of %d signal: %f\n', fig_num, SNR);
figure(fig_num);
subplot(2,1,1)
plot(signal);grid
title_str = ['The ', num2str(fig_num), ' Signal'];
title(title_str);
xlabel('Sample points');
ylabel('Amplitude');
subplot(2,1,2)
semilogx(f_x_axis, 20*log10(signal_spectrum_hanning.^2));grid
xlabel('Frequence (Hz)');
ylabel('Amplitude');
end
  1 个评论
昕毅
昕毅 2023-3-10
Because I think of SINAD as SNR(treat THD as noise), I compare my SNR caculation with built-in SINAD. When I set hanning 'periodic' and the cut-off frequency is half of the sampling frequency rather than a custom value(by changing cut_index), I got a similar answer.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Spectral Measurements 的更多信息

产品


版本

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by