How to generate a sine signal with changing frequency, but with predetermined number of elements and periods?

4 visualizaciones (últimos 30 días)
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;

Respuestas (2)

Shane
Shane el 11 de Ag. de 2015
  2 comentarios
matico
matico el 11 de Ag. de 2015
Thanks for sharing this.
In my case, the frequency should not change instantly, but it should change over some range. If I would have defined 2 frequencies (start at 1 Hz and finish at 50 Hz) it should "accelerate" through a whole range of 20 periods. If I have 3 frequencies [1 50 1], it should accelerate from start to the half and then slow down back to 1 Hz.

Iniciar sesión para comentar.


matico
matico el 12 de Ag. de 2015
Nobody else has any idea? I was trying to modify function, which was suggested by Shane, but with no success.

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by