【问题标题】:DFT code on Matlab does not work as intendedMatlab 上的 DFT 代码无法按预期工作
【发布时间】:2015-08-01 17:31:57
【问题描述】:

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

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)')   

【问题讨论】:

  • 我不明白 sI = sin(2*pi*f*Ts).*cos(sPhase); sQ = cos(2*pi*f*Ts).*sin(sPhase); 当我将它们更改为 sI = sin(2*pi*f*Ts+sPhase)+cos(2*pi*f*Ts+sPhase); sQ = cos(2*pi*f*Ts+sPhase)+sin(2*pi*f*Ts+sPhase); 时似乎正确
  • 我关于Hilbert transform 的第二条评论不是真的,所以我删除了它。也许我应该回顾这些课程:D

标签: matlab signal-processing fft dft


【解决方案1】:

原因在于两个不同的信号肯定会给你两个不同的频谱。查看下面的代码,你会发现你实际给出的dft算法的输入是sampledI+jsampledQ。因此,您在这里所做的不仅仅是将原始信号分解为In-phase and quadrature components,而是在此处执行Hilbert transform——将real 信号更改为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 应该是sampledInput = sI+1i*sQ;

【讨论】:

  • 是的,你是对的,我的错误是在 I Q 分量中也包含载波谐波。正确的方法应该是仅将信号的相位侧用于 IQ 分解。感谢您的帮助 Gnimuc 密钥。
猜你喜欢
  • 2014-12-06
  • 1970-01-01
  • 2012-07-04
  • 1970-01-01
  • 2011-08-01
  • 1970-01-01
  • 2013-04-05
  • 2021-10-11
  • 1970-01-01
相关资源
最近更新 更多