Matlab上的DFT代码无法正常工作

时间:2015-08-01 17:31:58

标签: matlab signal-processing fft dft

我正在尝试在Matlab上实现基本的DFT算法。 我只是使用具有相位调制的正弦波的相位和正交分量(增加频率a.k.a啁啾)。我将我的结果与Matlab的fft命令进行比较。只要没有相位调制(纯正弦),我的代码就会得到相同的结果。每当我添加啁啾调制时,结果都不同。例如,当我使用带有载波周围一些带宽的啁啾时,预期结果应该是从载波频率开始的啁啾带宽的频率分布。但是,我也从载波频率开始向后收集该结果的副本。您可以使用我的代码而无需修改任何内容。图5是我的结果,图6是预期结果。载波为256 Hz,带有10Hz的啁啾带宽。你可以看到下面的代码。重要的部分是循环,我采取信号的dft。你也可以在下面看到我的dft结果。

enter image description here

close all;
clear all;
%% signal generation
t = (0:0.0001:1); % 1 second window
f = 256; %freq of input signal in hertz
bw = 10; % bandwidth sweep of signal
phaseInput = 2*pi*t*bw.*t;                  
signalInput = sin(2*pi*f*t + phaseInput);   %input signal
inphase = sin(2*pi*f*t).*cos(phaseInput);    %inphase component
quadrature = cos(2*pi*f*t).*sin(phaseInput); %quadrature component

figure
plot(t,signalInput,'b',t,inphase,'g',t,quadrature,'r');
title('Input Signal');
xlabel('Time in seconds');
ylabel('Amplitude');

%% sampling signal previously generated
Fs = 1024; %sampling freq
Ts = (0:1/Fs:1);%sample times for 1 second window

sPhase = 2*pi*Ts*bw.*Ts;
sI = sin(2*pi*f*Ts).*cos(sPhase);
sQ = cos(2*pi*f*Ts).*sin(sPhase);

hold on;
plot(Ts,sI+sQ,'b*',Ts,sI,'g*',Ts,sQ,'r*');


fftSize = Fs; %Using all samples in dft
sampleIdx = (0:1:fftSize-1)';

sampledI = sI(1:fftSize)';
sampledQ = sQ(1:fftSize)';

figure;
plot(sampleIdx,sampledI,sampleIdx,sampledQ);
title('Sampled IQ Components');

%% DFT Calculation
dftI = zeros(fftSize,1);
dftQ = zeros(fftSize,1);

for w = 0:fftSize-1
    %exp(-2*pi*w*t) = cos(2*pi*w*t) - i*sin(2*pi*w*t)
    cI = cos(2*pi*w*sampleIdx/fftSize);     %correlation cos
    cQ = -sin(2*pi*w*sampleIdx/fftSize);    %correlation sin
    dftI(w+1) = sum(sampledI.*cI - sampledQ.*cQ); %
    dftQ(w+1) = sum(sampledI.*cQ + sampledQ.*cI);
end;

figure;
plot(Fs*sampleIdx/fftSize,dftI);
title('DFT Inphase');
xlabel('Hertz');
figure
plot(Fs*sampleIdx/fftSize,dftQ);
title('DFT Quadrature');
xlabel('Hertz');


figure;
plot(Fs*sampleIdx/fftSize,sqrt(dftQ.^2+dftI.^2));

%% For comparison
sampledInput = sin(2*pi*f*Ts + sPhase);

Y = fft(sampledInput(1:1024),1024);
Pyy = Y.*conj(Y)/1024;
f = (0:1023);
figure;
plot(f,Pyy)
title('Power spectral density')
xlabel('Frequency (Hz)')   

1 个答案:

答案 0 :(得分:0)

原因在于两个不同的信号肯定会给你两个不同的频谱。看看下面的代码,你会发现你实际给出的dft算法的输入是sampledI+jsampledQ。因此,您在此处所做的不仅仅是将您的原始信号分解为In-phase and quadrature components,而是在此处Hilbert transform - 将real信号更改为{{1}一个。

complex

所以用于比较的cI = cos(2*pi*w*sampleIdx/fftSize); %correlation cos cQ = -sin(2*pi*w*sampleIdx/fftSize); %correlation sin dftI(w+1) = sum(sampledI.*cI - sampledQ.*cQ); % dftQ(w+1) = sum(sampledI.*cQ + sampledQ.*cI); 应为sampledInput