Matlab code for sine wave with varying frequency
32 views (last 30 days)
Show older comments
How do I generate a sine wave with varying frequency and constant amplitude? I've tried the following code, but it is showing harmonics. How do i eliminate the harmoics?
clc;
fs = 2000; %sampling frequency
dt = 1/fs; %seconds per sample
StopTime = 10; %seconds
t = (0:dt:StopTime);
F = 50.*t; %Sinewave frequency {Hertz)
amp = 1000;
for i= 1:length(t);
data(i) = amp*sin(2*pi*F(i)*t(i));
end;
audiowrite('varFsin.wav', data, fs);
sound(data);
figure(1)
clf(1);
plot(t, data)
title('Sine Wave');
figure(2)
spectrogram(data, [80], [40], [256], fs, 'yaxis')
y = fft{data};
n = length(data); %number of samples
f = (0:n-1)*(fs/n); %frequency range
power = abs(y).^2/n; %power of the DFT
figure(3);
plot(f, power)
xlbel('frequency')
ylabel('power')
0 Comments
Accepted Answer
Chunru
on 1 Sep 2021
clc;
fs = 2000; %sampling frequency
dt = 1/fs; %seconds per sample
StopTime = 10; %seconds
t = (0:dt:StopTime);
F = 50.*t; %Sinewave frequency {Hertz)
amp = 1000;
The following is equivalent to LFM (linear frequency modulation) signal.
for i= 1:length(t);
data(i) = amp*sin(2*pi*F(i)*t(i));
end;
%audiowrite('varFsin.wav', data, fs);
sound(data);
figure(1)
clf(1);
plot(t, data)
title('Sine Wave');
figure(2)
spectrogram(data, [80], [40], [256], fs, 'yaxis')
y = fft(data);
n = length(data); %number of samples
f = (0:n-1)*(fs/n); %frequency range
power = abs(y).^2/n; %power of the DFT
figure(3);
powershift = fftshift(power);
fshift = (-n/2:n/2-1)*(fs/n); % zero-centered frequency range
plot(fshift,powershift)
xlabel('frequency')
ylabel('power')
This is not really harmonic. The spectrum has transition region around 0 and +/-fs/2. You have no harmonics to remove.
More Answers (0)
See Also
Categories
Find more on Fourier Analysis and Filtering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!