How should I generate a Matlab code for tenttone using the following information

6 views (last 30 days)
Syed Izzatullah
Syed Izzatullah on 25 Jan 2017
Edited: Image Analyst on 27 Jan 2017
TENTTONE Generates a triangularly shaped tone.
% S = TENTTONE(F,FS,LEN,PHASE) generates signal vector S that represents
% the samples of a triangularly shaped tone with frequency F in [Hz] and
% duration LEN in [sec]. Parameter FS determines the assumed underlying
% sampling frequency in [Hz]. Parameter PHASE is an optional argument
% that allows an explicit specification of a phase shift. Parameter
% PHASE defaults to zero if omitted.
%
% EXAMPLE: FS=8000; S=tenttone(440,FS,0.5,pi/4);
% sound(0.9*S,FS);
%
% See also RECTTONE and SINETONE.
% check for optional parameter
if nargin<4; PHASE=0; end
if nargin<4; PHASE=0; end

Answers (2)

Soumya Saxena
Soumya Saxena on 27 Jan 2017
I understand that you want to plot triangular wave forms. You may refer to the "sawtooth" function to plot a triangular wave. Please refer to the following documentation:
Another suggestion is to use "tripuls" function. For details you can refer to the following:
For examples on how to generate pulses, you may refer to this page:

Image Analyst
Image Analyst on 27 Jan 2017
Edited: Image Analyst on 27 Jan 2017
Use linspace(). Here, I did half of it for you. I trust you can do the other half.
% Program to create a wave file with variable amplitude and pitch.
% 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 = 12000;
duration = 1; % 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 * ones(1, length(t));
% Make a ramp
halfWay = round(length(t) / 2);
Amplitude(1:halfWay) = linspace(0, 32767, halfWay);
% 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

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!

Translated by