我正在尝试在GNU Octave中创建一个程序来绘制一个直方图,显示修改后的正弦波的基波和谐波(SCR调光器的输出,由一个正弦波组成,直到波的一部分为止)
我已经能够生成波形并执行FFT以获得一组频率与幅度点,但我不知道如何将此数据转换为适合生成直方图的区间。
clear();
vrms = 120;
freq = 60;
nCycles = 2;
level = 25;
vpeak = sqrt(2) * vrms;
sampleinterval = 0.00001;
num_harmonics = 10
disp("Start");
% Draw the waveform
x = 0 : sampleinterval : nCycles * 1 / freq; % time in sampleinterval increments
dimmed_wave = [];
undimmed_wave = [];
for i = 1 : columns(x)
rad_value = x(i) * 2 * pi * freq;
off_time = mod(rad_value, pi);
on_time = pi*(100-level)/100;
if (off_time < on_time)
dimmed_wave = [dimmed_wave, 0]; % in the dimmed period, value is zero
else
dimmed_wave = [dimmed_wave, sin(rad_value)]; % when not dimmed, value = sine
endif
undimmed_wave = [undimmed_wave, sin(rad_value)];
endfor
y = dimmed_wave * vpeak; % calculate instantaneous voltage
undimmed = undimmed_wave * vpeak;
subplot(2,1,1)
plot(x*1000, y, '-', x*1000, undimmed, '--');
xlabel ("Time (ms)");
ylabel ("Voltage");
% Fourier Transform to determine harmonics
subplot(2,1,2)
N = length(dimmed_wave); % number of points
fft_vals = abs(fftshift(fft(dimmed_wave))); % perform fft
frequency = [ -(ceil((N-1)/2):-1:1) ,0 ,(1:floor((N-1)/2)) ] * 1 / (N *sampleinterval);
plot(frequency, fft_vals);
axis([0,400]);
xlabel ("Frequency");
ylabel ("Amplitude");
答案 0 :(得分:0)
你知道你的基频(基音),我们称之为F
。 2*F
是二次谐波,3*F
是第三个谐波,等等。您想要在它们之间设置直方图边框:1.5*F
,2.5*F
等。
您的输入信号中有两个句点,因此您的(整数)基本频率为k=2
(fft_vals[k+1]
处的值,即图中的第一个峰值)。二次谐波位于k=4
,第三个位于k=6
,等等。
因此,您可以将容器边缘设置为k = 1:2:end
。
一般来说,这将是k = nCycles/2:nCycles:end
。
您可以根据我们计算的bin边缘计算条形图,如下所示:
fft_vals = abs(fft(dimmed_wave));
nHarmonics = 9;
edges = nCycles/2 + (0:nHarmonics)*nCycles;
H = cumsum(fft_vals);
H = diff(H(edges));
bar(1:nHarmonics,H);