📄 fmcw.m
字号:
%******************************************************************************
% fmcw.m
%
% Use: Code to generated triangular FMCW signals
%
% Inputs: Amplitude of the carrier signal
% Carrier signal frequency - f (Hz)
% Sampling frequency - fs (HZ)
% Desired SNR in dB
% Modulation bandwidth (deltaF)
% Modulation period (tm)
% Number of triangles in the signal
%
%
%Output: In-phase (I) and Quadarture (Q) components of the signal
% with 4 triangular periods.
%******************************************************************************
clc;clear all;
disp('*******************************************');
disp('******************* FMCW ********************');
disp('**********************************************');
% DEFAULT VARIABLES
A = 1; %Amplitude
f0 =1e3; %Carrier signal frequency - (Hz)
fs = 7e3; %Sampling frequency
SNR_dB = 0; %Desired SNR in dB
deltaF = 250; %Modulation bandwidth
tm = 20; %Modulation period
triangles=5; %No of triangles to be generated
sigma=1;
% NEW INPUT
newvar = 1;
while newvar == 1;
disp(' ')
disp('WHICH PARAMETER DO YOU WANT TO SET ? ')
disp(' ')
fprintf('1. Amplitude of the carrier signal - A= %g.\n', A)
fprintf('2. Carrier frequency - f0 (Hz)= %g.\n', f0)
fprintf('3. Sampling frequency - fs (Hz)=%g.\n',fs)
fprintf('4. Signal to noise ratio - SNR_dB (dB)= %g.\n', SNR_dB)
fprintf('5. Modulation bandwidth - deltaF (Hz)= %g.\n', deltaF)
fprintf('6. Modulation period - tm (milli-seconds)= %g.\n',tm)
fprintf('7. No changes\n')
disp(' ')
option= input('Select a option: ');
switch option
case 1
A=input('New Amplitude = ');
case 2
f0=input('Carrier frequency (Hz)=');
case 3
fs=input('New Sampling frequency (Hz)= ');
case 4
SNR_dB=input('New Signal to noise ratio (dB)= ');
case 5
deltaF=input('New modulation bandwidth (Hz)= ');
case 6
tm=input('New modulation period (milli-seconds)= ');
case 7
newvar = 0;
end
clc;
end
tm=tm/1000;
if SNR_dB ~= -inf
SNR = 10^(SNR_dB/20); %20log since we're dealing with voltage
sigma = A/(sqrt(2)*SNR); %Std. Dev. of noise required to achieve input SNR
else
SNR = 0; %For noise only, SNR = 0
sigma = 1; %If user wants noise only, let Std Dev = 1
end
ts = 1/fs; %Sample Period
time = (0:ts:(tm-ts))'; %Vector of sample times
%Build the pure sin & cos waveforms
I_carrier = cos(2*pi*f0.*time); % Carrier signals
Q_carrier = sin(2*pi*f0.*time); % Carrier signals
%sI1 is the In-Phase (I) transmitted signal for the up-ramp (without noise)
%sI2 is the In-Phase (I) transmitted signal for the down-ramp (without noise)
if SNR ~= 0
sI1 = A*cos(2*pi*((f0-deltaF/2).*time + deltaF/(2*tm).*time.^2));
sI2 = A*cos(2*pi*((f0+deltaF/2).*time - deltaF/(2*tm).*time.^2));
else
sI1 = zeros(length(time),1); %If noise only, Signal = 0
sI2 = zeros(length(time),1);
end
%Next two lines add req'd noise level to up & down ramps (I channel).
%Used to plot time sequence & PSD for each ramp
sI1_noisy = sI1 + sigma*randn(length(sI1),1);
sI2_noisy = sI2 + sigma*randn(length(sI2),1);
%Creates the I-channel time sequence of four up&down ramp pairs (NO noise)
I = [sI1;sI2;sI1;sI2;sI1;sI2;sI1;sI2];
%Adds the required noise to the time sequence
[a,b]=size(I);
SNR=10^(SNR_dB/10);
power=10*log10(A^2/(2*SNR));%calculate SNR in dB for WGN function
noise=wgn(a,b,power);%calculate noise at specified SNR
IwN=I+noise; %add noise to I with P4 phase shift
%sQ1 is the Quadrature (Q) transmitted signal for the up-ramp (without noise)
%sQ2 is the Quadrature (Q) transmitted signal for the down-ramp (without noise)
if SNR ~= 0
sQ1 = A*sin(2*pi*((f0-deltaF/2).*time + deltaF/(2*tm).*time.^2));
sQ2 = A*sin(2*pi*((f0+deltaF/2).*time - deltaF/(2*tm).*time.^2));
else
sQ1 = zeros(length(time),1); %If noise only, Signal = 0
sQ2 = zeros(length(time),1);
end
%Next two lines add req'd noise level to up & down ramps (Q channel).
%Used to plot time sequence & PSD for each ramp
sQ1_noisy = sQ1 + sigma*randn(length(sQ1),1);
sQ2_noisy = sQ2 + sigma*randn(length(sQ2),1);
%Creates the Q-channel time sequence of four up&down ramp pairs (NO noise)
Q = [sQ1;sQ2;sQ1;sQ2;sQ1;sQ2;sQ1;sQ2];
%Adds the required noise to the time sequence
QwN=Q+noise;
%*******************************************************
%PLOTS
%******************************************************
[uu,cc]=size(time);
disp(' ')
plt = input('Do you want to generate plots of the signal (Y/y or N/n) ?','s');
disp(' ')
if (plt == 'Y') | (plt =='y')
disp(' ')
%Plots time sequence & PSDs of In-Phase (cos) waveform
figure;
subplot(2,1,1);
plot(time(1:uu),I_carrier(1:uu)); grid;
title('Pure In-Phase Sinusoidal Carrier');
xlabel('Time (s)'); ylabel('Cos(2*pi*f_0*t)');
subplot(2,1,2);
psd(I_carrier,[],fs);
title('Power Spectral Density of Pure In-Phase Carrier');
%Plots time sequence & PSDs of Quadrature (sin) waveform
figure;
subplot(2,1,1);
plot(time(1:uu),Q_carrier(1:uu)); grid;
title('Pure Quadrature Sinusoidal Carrier');
xlabel('Time (s)'); ylabel('-Sin(2*pi*f_0*t)');
subplot(2,1,2);
psd(Q_carrier,[],fs);
title('Power Spectral Density of Pure Quadrature Carrier');
%Plots the triangular modulating waveform
% figure;
% plot(elapsed_time,f); grid;
% title('Triangular Modulating Signal')
% xlabel('Time (us)'); ylabel('Frequency (MHz)');
%Plots time sequence & PSD for I-channel up ramp
figure;
subplot(2,1,1);
plot(time, sI1_noisy); grid;
title(['In-Phase, Up-Ramp Transmitted Signal - SNR = ', num2str(SNR_dB), ' dB'])
xlabel('Time (s)'); ylabel('Signal');
subplot(2,1,2);
psd(sI1_noisy,[],fs);
title('Power Spectral Density of In-Phase, Up-Ramp Segment');
%Plots time sequence & PSD for I-channel down ramp
figure;
subplot(2,1,1);
plot(time, sI2_noisy); grid
title(['In-Phase, Down-Ramp Transmitted Signal- SNR = ', num2str(SNR_dB), ' dB'])
xlabel('Time (s)'); ylabel('Signal');
subplot(2,1,2);
psd(sI2_noisy,[],fs);
title('Power Spectral Density of In-Phase, Down-Ramp Segment');
%Plots time sequence & PSD for Q-channel up ramp
figure;
subplot(2,1,1);
plot(time, sQ1_noisy);grid;
title(['Quadrature, Up-Ramp Transmitted Signal - SNR = ', num2str(SNR_dB), ' dB'])
xlabel('Time (s)'); ylabel('Signal');
title(['Quadrature, Up-Ramp Transmitted Signal (Near End) - SNR = ', num2str(SNR_dB), 'dB'])
xlabel('Time (s)'); ylabel('Signal');
subplot(2,1,2);
psd(sQ1_noisy,[],fs);
title('Power Spectral Density of Quadrature, Up-Ramp Segment');
%Plots time sequence & PSD for Q-channel down ramp
figure;
subplot(2,1,1);
plot(time, sQ2_noisy); grid;
title(['Quadrature, Down-Ramp Transmitted Signal - SNR = ', num2str(SNR_dB), 'dB'])
xlabel('Time (s)'); ylabel('Signal');
subplot(2,1,2);
psd(sQ2_noisy,[],fs);
title('Power Spectral Density of Quadrature, Down-Ramp Segment');
else
disp('Signal not plotted')
fprintf('\n\n')
end
%Saves the I & Q vectors in a .mat file for processing by the receiver folks
disp(' ')
saveresult = input('Do you want to save the new signal (Y/y or N/n) ?','s');
if (saveresult == 'Y') | (saveresult =='y')
I2=I; Q2=Q;
I=IwN; Q=QwN;
ff=floor(f0/1e3);
ffs=floor(fs/1e3);
save(['F_', num2str(ff), '_', num2str(ffs), '_', num2str(deltaF), '_', num2str(tm*1000), '_',num2str(SNR_dB)],'I','Q');
I=I2;
Q=Q2;
save(['F_' num2str(ff) '_' num2str(ffs) '_' num2str(deltaF) '_' num2str(tm*1000) '_s'],'I','Q');
disp(' ');
disp(['Signal and noise save as : F_' num2str(ff) '_' num2str(ffs) '_' num2str(deltaF) '_' num2str(tm*1000) '_' num2str(SNR_dB)]);
disp(['Signal only save as : F_' num2str(ff) '_' num2str(ffs) '_' num2str(deltaF) '_' num2str(tm*1000) '_s']);
disp(['Directory: ' num2str(cd)]);
else
disp(' ')
disp('Signal not saved')
fprintf('\n\n')
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -