Creating a sinewave with logarithmic frequency
显示 更早的评论
I'm trying to generate a sinewave with logarithmic increasing frequency.
freq = f_str*((f_end/f_str).^(time/T));
The max frequency in the sinewave is 50 Hz. However, when I construct such sinewave and run a spectrogram on it (STFT), it shows that the max frequency is 150 Hz.
Any thoughts would be appreciated.
% Generate log-frequency sweep then plot it and its spectrogram
NPTS = 225000;
f_str = 7;
f_end = 50;
T = 450;
time = linspace(0,T,NPTS)';
freq = f_str*((f_end/f_str).^(time/T));
Y = sin(2*pi*freq.*time); % Sinewave with logarithmic sweep
fs = (2*NPTS)/900;
subplot(311), plot(time, freq, '-b', 'LineWidth',3), grid on, xlabel('Time [sec]'), ylabel('Frequency [Hz]')
subplot(312), plot(time, Y, '-b', 'LineWidth',3), grid on, xlabel('Time [sec]'), ylabel('SineWave [-]'), axis([0 450 -2 2])
subplot(313), stft(Y, fs, 'FrequencyRange', 'onesided'), set(gca,'Ylim',[0 200]), colorbar off
4 个评论
% Generate log-frequency sweep then plot it and its spectrogram
NPTS = 225000;
f_str = 7;
f_end = 50;
T = 450;
time = linspace(0,T,NPTS)';
freq = f_str*((f_end/f_str).^(time/T));
Y = sin(2*pi*freq.*time); % Sinewave with logarithmic sweep
fs = (2*NPTS)/900;
subplot(311), plot(time, freq, '-b', 'LineWidth',3), grid on, xlabel('Time [sec]'), ylabel('Frequency [Hz]')
subplot(312), plot(time, Y, '-b', 'LineWidth',3), grid on, xlabel('Time [sec]'), ylabel('SineWave [-]'), axis([0 450 -2 2])
subplot(313), stft(Y, fs, 'FrequencyRange', 'onesided'), set(gca,'Ylim',[0 200]), colorbar off
subplot(312),xlim([449.9 450])
Shows the time series generated 15 cycles/0.1 sec --> 150 Hz so stft isn't lying; you just don't have the input signal you think you have...
Djamil Boulahbal
2024-10-25
% Generate log-frequency sweep then plot it and its spectrogram
NPTS = 225000;
f_str = 7;
f_end = 50;
T = 450;
time = linspace(0,T,NPTS)';
freq = f_str*((f_end/f_str).^(time/T));
Y = sin(2*pi*freq.*time); % Sinewave with logarithmic sweep
fs = (2*NPTS)/900;
tView=[0 1];
y=chirp(time,f_str,T,f_end,"logarithmic",-90);
subplot(321), plot(time, Y, '-b', 'LineWidth',1), grid on, xlabel('Time [sec]'), ylabel('Original'), axis([0 450 -2 2])
xlim(tView)
subplot(323), plot(time, y, '-b', 'LineWidth',1), grid on, xlabel('Time [sec]'), ylabel('Chirp'), axis([0 450 -2 2])
xlim(tView)
tView=[T-0.1 T];
subplot(322), plot(time, Y, '-b', 'LineWidth',1), grid on, xlabel('Time [sec]'), ylabel('Original'), axis([0 450 -2 2])
xlim(tView)
subplot(324), plot(time, y, '-b', 'LineWidth',1), grid on, xlabel('Time [sec]'), ylabel('Chirp'), axis([0 450 -2 2])
xlim(tView)
Compares to using the Signal Processing TB chirp function -- I didn't have time to try to dig into just what the root cause is in the straightforward calculation; perhaps the issue of the large angle is part, but the internals of the chirp function deal with it and do produce the expected number of cycles over the last 100 msec of the time history whereas the original clearly shows that the frequency of the actual time trace itself is right at the 150 Hz, not the expected 50 Hz. OTOMH, I don't have any better explanation, only that one can illustrate the result...
Djamil Boulahbal
2024-10-25
采纳的回答
更多回答(2 个)
Djamil Boulahbal
2024-10-25
编辑:Djamil Boulahbal
2024-10-25
0 个投票
1 个评论
But the sin function itself deals with long time series ok it seems...
% Generate log-frequency sweep then plot it and its spectrogram
NPTS = 225000;
f_str = 7;
f_end = 50;
T = 450;
time = linspace(0,T,NPTS)';
%freq = f_str*((f_end/f_str).^(time/T));
%Y = sin(2*pi*freq.*time); % Sinewave with logarithmic sweep
Y = sin(2*pi*f_str*time); % Sinewave with starting frequency
tView=[0 1];
y = sin(2*pi*f_end*time); % Sinewave with ending frequency
subplot(321), plot(time, Y, '-b', 'LineWidth',1), grid on, xlabel('Time [sec]'), ylabel('Start F'), axis([0 450 -2 2])
tView=[0 1]; xlim(tView)
subplot(322), plot(time, y, '-b', 'LineWidth',1), grid on, xlabel('Time [sec]'), ylabel('End F'), axis([0 450 -2 2])
tView=[T-0.1 T];xlim(tView)
subplot(323), plot(time, Y, '-b', 'LineWidth',1), grid on, xlabel('Time [sec]'), ylabel('Start F'), axis([0 450 -2 2])
tView=[0 1]; xlim(tView)
subplot(324), plot(time, y, '-b', 'LineWidth',1), grid on, xlabel('Time [sec]'), ylabel('End F'), axis([0 450 -2 2])
tView=[T-0.1 T];xlim(tView)
The first 1 sec shows 7 cycles, the last 100 msed is 5, so the two full-length calculations of a fixed-frequency sine wave over the same t vector produce the expected frequencies. Hence one must conclude it isn't internal rounding in the sin() function itself causing the difference in the original code..
As before, I don't have time to really dig, but can show some results that are expected to compare against...
类别
在 帮助中心 和 File Exchange 中查找有关 Title 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





