Increasing the amplitude of the sine

76 Ansichten (letzte 30 Tage)
Jack Walker
Jack Walker am 19 Apr. 2016
Kommentiert: Image Analyst am 24 Jan. 2022
Hello ! Is it possible to increase the amplitude of sine and then plot the sine ? Basically i want to plot a sine with a small amplitude which is getting bigger over the time. And last, i want to make it visible, so i can see the change of the amplitude. I thought about using a for loop, but it didn't worked out like i imagined. (Btw English is not my first language, sorry about that..)

Akzeptierte Antwort

Image Analyst
Image Analyst am 19 Apr. 2016
Try this:
% Define a time axis:
t = 0 : 0.1 : 20;
% Define the period
period = 3;
% Make a function for how the amplitude varies with time:
% For example the amplitude is the square root of time,
% or whatever formula you want to use.
amplitude = sqrt(t);
% Now make the sine wave
y = amplitude .* sin(2 * pi * t / period);
plot(t, y, 'b-');
grid on;
  7 Kommentare
yanqi liu
yanqi liu am 24 Jan. 2022
t = 0 : 0.01 : 20;
% Define the period
period = 3;
% Make a function for how the amplitude varies with time:
% For example the amplitude is the square root of time,
% or whatever formula you want to use.
amplitude = sqrt(t);
% Now make the sine wave
y = amplitude .* sin(2 * pi * t / period);
plot(t, y, 'b-');
grid on;
sound(y)
Image Analyst
Image Analyst am 24 Jan. 2022
3 Hz is way too low a frequency of sound to hear. You'll need to have a higher frequency. Try this attached demo to see how to make a sound that you can hear:
% Program to create a warbling wave file with variable amplitude and pitch.
% function make_wav_file()
% Initialization / clean-up code.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Create the filename where we will save the waveform.
folder = pwd;
baseFileName = 'Test_Wave.wav';
fullFileName = fullfile(folder, baseFileName);
fprintf('Full File Name = %s\n', fullFileName);
% Set up the time axis:
Fs = 8000;
duration = 2; % seconds.
t = 1 : duration * Fs; % 2 seconds
% Set up the period (pitch, frequency):
T = 13; % Constant pitch if you use this.
T = linspace(25, 8, length(t)); % Pitch changes if you use this.
% Create the maximum amplitude:
Amplitude = 32767;
% Add an exponential decay:
Amplitude = Amplitude .* exp(-0.0003*t);
% Add an ocillation on the amplitude:
% Amplitude = Amplitude .* rand(1, length(x)); % Makes a shushing/roaring sound.
Amplitude = Amplitude .* sin(2.*pi.*t./2000); % Decaying pulsing sound.
% Construct the waveform:
y = int16(Amplitude .* sin(2.*pi.*t./T));
% y = abs(int16(Amplitude .* sin(2.*pi.*x./T)));
% Plot the waveform:
plot(t, y, 'b-');
title('Waveform', 'FontSize', fontSize);
xlabel('Time', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
grid on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
fprintf('Writing file %s...\n', fullFileName);
% Write the waveform to a file:
audiowrite(fullFileName, y, Fs);
% Play the sound as many times as the user wants.
playAgain = true;
counter = 1;
while playAgain
% Play the sound that we just created.
fprintf('Playing file %s %d times...\n', fullFileName, counter);
player = audioplayer(y, Fs);
play(player);
% Ask user if they want to play the sound again.
promptMessage = sprintf('You have played the sound %d times.\nDo you want to play the sound again?', counter);
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Yes', 'No', 'Yes');
if strcmpi(button, 'No')
playAgain = false;
break;
end
counter = counter + 1;
end
% Alert user that we are done.
message = sprintf('Done playing %s.\n', fullFileName);
fprintf('%s\n', message);
promptMessage = sprintf('Done playing %s.\nClick OK to close the window\nor Cancel to leave it up.', fullFileName);
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'OK', 'Cancel', 'OK');
if strcmpi(button, 'OK')
close all; % Close down the figure.
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Robert Steidl
Robert Steidl am 19 Apr. 2016
I possibly do have a solution for your Problem - with minor Settings to solve it! Maybe it is a good init to your case!
At first I have declined my time variable for three Sine - then I set a variable over one period that changes in my for Loop! At the end I have three sine with three different amplitudes in a row!
if true
t=0:pi/16:6*pi;
temp = 1;
for i=1:3
k(temp:find(t==i*2*pi))=i;
temp=length(k);
end
y=k.*sin(t);
plot(t,y)
end
Hope that helps
  1 Kommentar
Jack Walker
Jack Walker am 19 Apr. 2016
Hi Robert ! Thank you very much for your answer. It was exactly what i needed. But could you explain, what the term " k(temp:find(t==i*2*pi))=i;" means ? I have a vague idea, but i am not quite sure. Sorry for bothering you !

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Audio I/O and Waveform Generation finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by