How can I generate a sine wave with different freq

2019-04-12 21:31发布

问题:

For my project I need to generate a sine wave using matlab which has 100 000 samples and the frequency changes randomly after every 10 000 samples. The sampling rate and the frequencies can be as per convenience. Is there any function in matlab to generate this?

回答1:

OK another example: to generate 5 randon frequencies :-)

%range of possibles frequencies
FrequenciesRandon = [200:1:500];

%number of randon frequencies ??
nf = 5;

EndSignal=[];

for j = 1 : nf
    t  = [ 0 : 1 : 10000];           % Time Samples
    f=randsample(FrequenciesRandon,1); % get the randon frequencie
    Fs = 44100;                     % Sampling Frequency
    data = sin(2*pi*f/Fs*t)';        % Generate Sine Wave
    EndSignal= [data;EndSignal];    
end

wavplay(EndSignal,Fs) 


回答2:

Hello I know of no ready function to do this in matlab, but do it in matlab is quite simple, a simple example of how to generate 10 000 samples in 450Hz

t  = [ 0 : 1 : 10000];           % Time Samples
f  = 450;                       % Input Signal Frequency
Fs = 44100;                     % Sampling Frequency
data = sin(2*pi*f/Fs*t)';        % Generate Sine Wave
wavplay(data,Fs)                 %to Listen


回答3:

Here is an example for different sequential frequencies.

% Generate a sequencial sinusoid
fs = 8000;                                        % sampling rate
amp = 1;                                          % amplitude
freqs = [262, 294, 330, 350, 392, 440, 494, 523]; % frequency in Hz
T = 1/fs;                                         % sampling period
dur = 0.5;                                        % duration in seconds
phi = 0;                                          % phase in radian
y = [];

for k = 1:size(freqs,2)
    x = amp*sin(2*pi*freqs(k)*[0:T:dur-T]+phi);
    y = horzcat(y,x);
end