How to generate a sine signal with changing frequency, but with predetermined number of elements and periods?
5 views (last 30 days)
Show older comments
Hi.
I have to generate (plot) a sine signal which should have predetermined number of elements (lat say 200000) and predetermined number of periods (20). That means, 10000 elements per period.
The function (or a script) should also provide an option to freely select how the frequency is changing between those 20 periods. Actually, I want to change frequency anywhere between 1 Hz and 50 Hz. Let say, from 1 Hz to 50 Hz and then back to 1 Hz. Or, from 1 to 5 Hz in first 15 periods and then accelerate to 50 Hz in last 5 period. Possibilities are endless, but first two conditions (num. of elements and periods) should always be met.
I have made a script, but this is not exactly what I want. The code is attached below. Problem here is, that the number of periods is not OK. Here I am changing a frequency with values in array "Freq". If Freq = [1 20 1], then frequency will change from 1 to 20 (in the middle) and back to 1 Hz. This way I can change a frequency as I want, but the number of periods is not constant (20).
I would be very grateful, if someone could help me with that problem.
Thank you in advance.
clear all;
Freq = [1 20 1];
% Number of elements
Fs = 200000;
Ts = 1/Fs;
T = 0:Ts:1-Ts;
N = Fs / (length(Freq)-1);
switch numel(Freq)
case 1
FreqArray(1,1:Fs) = Freq(1);
case 2
FreqArray = linspace(Freq(1), Freq(2), N);
case 3
FreqArray = [linspace(Freq(1), Freq(2), N) linspace(Freq(2), Freq(3), N)];
case 4
FreqArray = [linspace(Freq(1), Freq(2), N) linspace(Freq(2), Freq(3), N)...
linspace(Freq(3), Freq(4), N+2)];
case 5
FreqArray = [linspace(Freq(1), Freq(2), N) linspace(Freq(2), Freq(3), N)...
linspace(Freq(3), Freq(4), N) linspace(Freq(4), Freq(5), N)];
case 6
FreqArray = [linspace(Freq(1), Freq(2), N) linspace(Freq(2), Freq(3), N)...
linspace(Freq(3), Freq(4), N) linspace(Freq(4), Freq(5), N)...
linspace(Freq(5), Freq(6), N)];
case 7
FreqArray = [linspace(Freq(1), Freq(2), N) linspace(Freq(2), Freq(3), N)...
linspace(Freq(3), Freq(4), N) linspace(Freq(4), Freq(5), N)...
linspace(Freq(5), Freq(6), N) linspace(Freq(6), Freq(7), N+2)];
end
Phi = 2 * pi * cumsum(FreqArray) / length(T);
Sin = sin(Phi);
plot(T, Sin); grid;
0 Comments
Answers (2)
Shane
on 11 Aug 2015
2 Comments
rasam aliazizi
on 19 Apr 2019
Did you get any luck on this? If so would you mind sharing your findings?
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!