Creating a Triangle Wave woes
Show older comments
I'm having trouble generating a nice looking triangle wave. Doing the calculation, with what I think is, the proper way results in a distorted PSD of the signal. Doing the Triangle in another way (with time reversal), gives a much better PSD image.
However, when these two signals are loaded into a VSG, the proper way has the correct period, whereas the second way has a period of 1/2.
In the following code, I'll show the 2 methods used. When loaded loaded into the VSG, the first has spectral spacing of 1 KHz (which is expected with a rep_rate of 1000). However, the second (with the better PSD) has a spectral spacting of 2 KHz. I have been unable to determine what is causing the issues with either method.
bandwidth = 20e6; %20 MHz
sample_rate = 100e6; %100MHz
rep_rate = 1000; %1000 times a second
num_samples = sample_rate/rep_rate;
%method 1
time_vector = 1/sample_rate : 1/sample_rate: 1/(rep_rate);
mid_time = round(length(time_vector)/2);
%linear rate at which the sweep increases in frequency
inc_chirp_rate = (2*band_width*sample_rate)/num_samples;
dec_chirp_rate = -1*((2*band_width*sample_rate)/num_samples);
%freqency which the saw sweep starts at
inc_start_freq = -1*(band_width/2);
dec_start_freq = 3*band_width/2;
%A linear sweep argument fits the following format:
% 2*pi( start_freq + (chirp_rate/2)*t)*t
inc_sweep_values = 2*pi*( inc_start_freq + (inc_chirp_rate/2).*time_vector(1:mid_time) ).*time_vector(1:mid_time);
dec_sweep_values = 2*pi*( dec_start_freq + (dec_chirp_rate/2).*time_vector(mid_time + 1:end)).*time_vector(mid_time + 1:end);
%Complete signal method 1
complete_val = [inc_sweep_values dec_sweep_values];
%create the I & Q followed by the Complex combination of the signal
i1 = cos(complete_val);
q1 = sin(complete_val);
com1 = complex(i1,q1);
%complete signal method 2
%uses the time reversal of the incline
complete_val2 = [inc_sweep_values inc_sweep_values(end:-1:1)];
%calculate the I & Q and then complex signal for the 2nd method
i2 = cos(complete_val2);
q2 = sin(complete_val2);
com2 = complex(i2,q2);
%compare the signals
%resample the signals for easier viewing
[f1, p1] = freqz(com1, 1, 1024 ,'whole', sample_rate);
[f2, p2] = freqz(com2, 1, 1024 ,'whole', sample_rate);
%take the log of the fft of the signal
logdata1 = 20*log10(fftshift(abs(f1)));
logdata2 = 20*log10(fftshift(abs(f2)));
%plot the 2 signals
figure;
plot(logdata1);
title('Formal way to calculate Tri');
figure;
plot(logdata2);
title('Time reveral way to calculate');
Accepted Answer
More Answers (0)
Categories
Find more on Parametric Spectral Estimation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!