How to convert sawtooth output signal to sine in matlab

27 views (last 30 days)
Hello there,
I have this sawtooth signal but I want to convert it into sine with matlab , any advice how to do so?
  2 Comments
Mathieu NOE
Mathieu NOE on 2 Mar 2022
hello
I see two options
  • you can use a bandpass filter to remove the higher harmonics of the sawtooth signal and keep only the first harmonic = sine
  • you can apply a linear (affine) correction law (like out = a * in + b) to make your sawtooth signal match as accurately as possible the range 0 to 2*pi and then use sin or cos trigonometric function.
Adam Danz
Adam Danz on 2 Mar 2022
To add to Mathieu's options,
  • If you know the period, phase, and amplitude of the signal you could use those to produce a sin function.

Sign in to comment.

Answers (1)

Abhimenyu
Abhimenyu on 3 Nov 2023
Hi Sam,
I understand that you want to convert the sawtooth waveform obtained from your analysis to a sine wave using MATLAB. If the frequency of the sawtooth waveform is known, the sine wave can be produced easily.
The “fft” function of MATLAB can be used to find the fundamental frequency component of the sawtooth waveform. Using the fundamental component, sine wave can be constructed.
Please refer to example MATLAB code below to understand more:
% Parameters
fs = 1000; % Sampling frequency (Hz)
t = 0:1/fs:1; % Time vector
% Generate a sawtooth signal
frequency = 5; % Frequency of the sawtooth wave (Hz)
sawtooth_wave = sawtooth(2 * pi * frequency * t);
% Perform Fourier analysis to extract the fundamental frequency component
sawtooth_fft = fft(sawtooth_wave);
frequencies = fs * (0:(length(sawtooth_fft)/2))/length(sawtooth_fft);
[~, index] = max(abs(sawtooth_fft(1:floor(length(sawtooth_fft)/2)))); % Find the dominant frequency component
fundamental_frequency = frequencies(index);
% Generate a sine wave with the same fundamental frequency
sine_wave = sin(2 * pi * fundamental_frequency * t);
% Plot the original sawtooth wave and the converted sine wave
subplot(2,1,1);
plot(t, sawtooth_wave);
title('Original Sawtooth Wave');
subplot(2,1,2);
plot(t, sine_wave);
title('Converted Sine Wave');
A similar approach can be applied to the sawtooth waveform obtained from your analysis to obtain the sine wave.
To know more about the “fft” function, please refer to the below mentioned MATLAB documentation link:
I hope this helps to resolve the query.
Thanks,
Abhimenyu
  1 Comment
Mathieu NOE
Mathieu NOE on 6 Nov 2023
hello
nice answer but the fft based solution is limited by the frequency resolution you can obtain, depending of the sampling rate and record length
df = fs/samples (and you can take nfft = samples for best resolution)
here in this example fs = 1000 and you have 1001 samples so df is around 1 Hz which is very coarse
of course you can be lucky and have your signal frequency exactly matching the fft frequency vector bins , but in general this would nott be the case
if you run your code with a sawtooth signal generated at another value, for example 5.423789 instead of 5 Hz, your fft computed frequency is still 4.9950 Hz; NB that it's 4.9950 Hzand not 5 Hz because you generated 1001 (and not 1000) samples
IMHO you can easily overcome that frequency resolution limitation by calculating the time delta between successive threshold crossing points. This is by far much more precise , as yu can see below :
format long
% Parameters
fs = 1000; % Sampling frequency (Hz)
t = 0:1/fs:1; % Time vector
% Generate a sawtooth signal
frequency = 5.123789; % Frequency of the sawtooth wave (Hz)
sawtooth_wave = sawtooth(2 * pi * frequency * t);
% signal rate = time index of threshold crossing points
threshold = 0; %
[t0_pos1,t0_neg1] = find_zc(t,sawtooth_wave,threshold);
fundamental_frequency = 1./mean(diff(t0_pos1))
fundamental_frequency =
5.123789000000000
% Generate a sine wave with the same fundamental frequency
sine_wave = sin(2 * pi * fundamental_frequency * t);
% Plot the original sawtooth wave and the converted sine wave
figure(1)
plot(t, sawtooth_wave, t, sine_wave);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ZxP,ZxN] = find_zc(x,y,threshold)
% positive slope "zero" crossing detection, using linear interpolation
y = y - threshold;
zci = @(data) find(diff(sign(data))>0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
ZxP = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
% negative slope "zero" crossing detection, using linear interpolation
zci = @(data) find(diff(sign(data))<0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
ZxN = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!