How can I generate a sine wave with different frequencies using matlab?

十年热恋 提交于 2019-12-06 11:46:08

问题


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


来源:https://stackoverflow.com/questions/9199019/how-can-i-generate-a-sine-wave-with-different-frequencies-using-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!