chirp signal generation indicates invalid sampling rate - matlab

good evening guys, i want to generate chirp signal with frequency sweeps from 10 Hz up to 35 Hz for 2 second, sampling frequency is 100, here is my code
fs =100;
dt =1/fs;
%dt =0.01;
t =0:dt:2;
f0 =10;
f1 =35;
t1=2;
x =chirp(t,f0,t1,f1,'quadratic');
%Fs = round(1/mean(diff(t)));
%sound(x,Fs);
plot(t,x)
%spectrogram(x,128,120,128,100,'yaxis');
%colormap jet
but when i try to play it using following code :
sound(x,fs)
i got following error :
>> chirp_signal
Error using sound (line 79)
Device Error: Invalid sample rate
Error in chirp_signal (line 14)
sound(x,fs)
i can't get point where mistake, could you help me please?

Related

Delay-based audio effects processor

I'm doing a script to implement flanger sound effect in octave.
I'm having the following error at line 33:
error: 'ampx' undefined near line 33, column 33
error: called from
flanger at line 33 column 6
Can someone help me? I'll post script next.
Thanks in advance
close all
clear all
clc
%flanger.m
% Creates a single FIR delay with the delay time oscillating from
% Either 0-3 ms or 0-15 ms at 0.1 - 5 Hz
infile="acoustic.wav";
[som, fs]=audioread(infile); %som-som, fs sampling rate
#outfile=audiowrite('out_flanger.wav');
% read the sample waveform
[som, fs] = audioread(infile);
% parameters to vary the effect %
max_time_delay=0.003; % 3ms max delay in seconds
rate=1; %rate of flange in Hz
index=1:length(som);
% sin reference to create oscillating delay
sin_ref = (sin(2*pi*index*(rate/fs)))';
%convert delay in ms to max delay in samples
max_samp_delay=round(max_time_delay*fs);
% create empty out vector
y = zeros(length(som),1);
% to avoid referencing of negative samples
y(1:max_samp_delay)=som(1:max_samp_delay);
% set amp suggested coefficient from page 71 DAFX
amp=0.7;
% for each sample
for i = (max_samp_delay+1):length(som),
cur_sin=abs(sin_ref(i)); %abs of current sin val 0-1
% generate delay from 1-max_samp_delay and ensure whole number
cur_delay=ceil(cur_sin*max_samp_delay);
% add delayed sample
y(i) = (ampx(i)) + amp*(som(i-cur_delay)); %%LINE 33!!!!!!!
end
% write output
audiowrite(outfile,y,Fs);

FFT shows two time bigger frequency -- why? [duplicate]

I'm creating a sweep / chirp signal using matlab / octave and my ending signal seems to be ending at the wrong frequency. How can I fix it so that the signal ends at the correct frequency.
PS: I can't use the chirp command in octave because I'm creating a chirp / sweep signal using a specific equation.
Example code with simple equation. and plot of problem
%test sweep / chirp
clear all,clc
freq1=20; %start freq
freq2=200; %end freq
fs=44100;
dur=1; %duration of signal in seconds
t = linspace(0,2*pi,fs*dur);
f=freq1:(freq2-freq1)/length(t):freq2-(freq2-freq1)/length(t);
%20:(200-20)/lenght(t) :200-(200-20)/length(t)
data=sin(f.*t); %build signal
data=(data/max(abs(data))*.8); %normalize signal
wavwrite([data'] ,fs,32,strcat('/tmp/del.wav')); %export file
plot(t,data)
PS: I'm using octave 3.8.1
The following code explains how to generate a frequency-variable sin wave.
freq1=20; %start freq
freq2=200; %end freq
dur=1; %duration of signal in seconds
freq=#(t)freq1+(freq2-freq1)/dur*t;
%another example, may help to understand the code
%dur=2
%freq=#(t)heaviside(t-1)*10+heaviside(t-1.5)*-9;
%Integerate over the time-local frequency, gives the average frequency until t which later on gives the sin with the right phase
%In case you don't have symbolic toolbox, integrate manually. For the given numbers Ifreq=#(x)x.*(x.*9.0+2.0)
Ifreq=matlabFunction(int(freq(sym('x'))));
%Defining wave function based on `Ifreq`
wave=#(t)(sin(Ifreq(t)*2*pi));
t=0:.00001:dur;
plot(t,wave(t));
Following Daniel's recipe, this is a version that uses numerical integration, and consequently doesn't require the symbolic toolbox:
freq1 = 20; % start frequency
freq2 = 200; % end frequency
fs = 44100;
dur = 1; % duration of signal in seconds
t = 0:1/fs:dur;
freqt = linspace(freq1,freq2,numel(t));
ifreqt = cumsum(freqt)/fs;
data = sin(2*pi*ifreqt);
plot(t,data);

Nyquist theorem in matlab

I'm trying to make some small scripts in matlab, so I can hear both the analog and digital sine waves, but I’m confused and with 2 problems
In the analog code the idea is to be able to change Tm that is the sampling period, by changing Tm so I can choose whatever samples the user wants. However I'm stuck with the stem function, since I haven't been able to change the sample rate in the stem() function
In the digital code, I’m trying to make digital sound code come out from the speakers, and I did however, I’m not even sure the sound is actually digital, since when using N=2^1 the sound can be hear at really good quality, with a little noise, (when it’s supposed to play that way only at 8 bits)
Hopefully someone here can lend me a hand.
-------------------- FOR THE ANALOGIC SOUND
clf
t=0:1:17.7
y=sin(2*pi*0.06*t) %// l von Vp de 2.5v
plot(t,y) %// Entry signal
hold on
plot(t,y,'ko') %// Output graph
stem(t,y)
hold off
n=[0:1:10000] %// Duration of tone
ftono=440 %// sound frequency
fm=8000 %// frecuency sample rate
Tm=1/fm %// sampling period
A=1
x=A*sin(2*pi*ftono*Tm*n) %// Sin wave using sam,pling period
sound(x,fm) %// Analogic sound
-------------------- FOR THE DIGITAL SOUND (change N from 2^1 to 2^16)
clf
t = 0:1:1600
fm = 1000
Tm=1/fm
ftono = 440
N=2^2
senial = sin(2*pi*t*ftono*Tm)
y = round(senial*N)/N
plot(round(sin(2*pi*t/1000)*N)/N)
sound(round(sin(2*pi*t*ftono*Tm)*N)/N, 1000)
First of you should understand that both of your snippets result in digital sound. This is not a result of your code. Since you reproduce sound from digital information, the sound is "digital". Thus, if both of your sounds have the same sampling frequency then both will be heard the same.
Judging from your question's subject, you should be trying to reproduce the aliasing effect caused by insufficient sampling frequencies or you should be trying to reproduce quantization noise caused by small sample word length.
The first of my assumptions is based on the sampling frequency and the second on the "8 bits" part of your answer.
You can demo both of these functionalities in the code below.
function adsoundtest(dur, freq, nbits, amp, fs)
%Reproduce a simulated analogue sound and a digital one
% ADSOUNTEST(DUR, FREQ, NBITS, AMP, FS)
% Duration (dur) is in seconds, freq is the signal's
% frequency in Hz, nbits is the number of bits for
% sample length, amp is the amplification of the sine
% wave (max 1) and fs is the sampling frequency in Hz.
fs_analogue = 100 * freq;
if nargin < 5
if nargin < 4
if nargin < 3
if nargin < 2
error('ERROR:ADSOUNDTEST', 'Too few input arguments');
end
nbits = 16;
end
amp = 1;
end
fs = freq * 3;
end
fs_digital = fs;
t_analogue = 0:1/fs_analogue:(dur - 1/fs_analogue);
t_digital = 0:1/fs_digital:(dur - 1/fs_digital);
w = 2 * pi * freq;
x_analogue = amp .* sin(w .* t_analogue);
x_digital = amp .* sin(w .* t_digital);
try
ymax = intmax(['int' num2str(nbits)]);
ymin = intmin(['int' num2str(nbits)]);
catch me
disp 'Number of bits not supported. Try 64, 32, 16 and 8';
rethrow(me)
end
x_digital = round(mapminmax(x_digital, double(ymin), double(ymax)));
sound(x_analogue, fs_analogue);
disp('Press enter to continue')
pause;
sound(x_digital, fs_digital);
end

Sweep / chirp signal ends at incorrect frequency

I'm creating a sweep / chirp signal using matlab / octave and my ending signal seems to be ending at the wrong frequency. How can I fix it so that the signal ends at the correct frequency.
PS: I can't use the chirp command in octave because I'm creating a chirp / sweep signal using a specific equation.
Example code with simple equation. and plot of problem
%test sweep / chirp
clear all,clc
freq1=20; %start freq
freq2=200; %end freq
fs=44100;
dur=1; %duration of signal in seconds
t = linspace(0,2*pi,fs*dur);
f=freq1:(freq2-freq1)/length(t):freq2-(freq2-freq1)/length(t);
%20:(200-20)/lenght(t) :200-(200-20)/length(t)
data=sin(f.*t); %build signal
data=(data/max(abs(data))*.8); %normalize signal
wavwrite([data'] ,fs,32,strcat('/tmp/del.wav')); %export file
plot(t,data)
PS: I'm using octave 3.8.1
The following code explains how to generate a frequency-variable sin wave.
freq1=20; %start freq
freq2=200; %end freq
dur=1; %duration of signal in seconds
freq=#(t)freq1+(freq2-freq1)/dur*t;
%another example, may help to understand the code
%dur=2
%freq=#(t)heaviside(t-1)*10+heaviside(t-1.5)*-9;
%Integerate over the time-local frequency, gives the average frequency until t which later on gives the sin with the right phase
%In case you don't have symbolic toolbox, integrate manually. For the given numbers Ifreq=#(x)x.*(x.*9.0+2.0)
Ifreq=matlabFunction(int(freq(sym('x'))));
%Defining wave function based on `Ifreq`
wave=#(t)(sin(Ifreq(t)*2*pi));
t=0:.00001:dur;
plot(t,wave(t));
Following Daniel's recipe, this is a version that uses numerical integration, and consequently doesn't require the symbolic toolbox:
freq1 = 20; % start frequency
freq2 = 200; % end frequency
fs = 44100;
dur = 1; % duration of signal in seconds
t = 0:1/fs:dur;
freqt = linspace(freq1,freq2,numel(t));
ifreqt = cumsum(freqt)/fs;
data = sin(2*pi*ifreqt);
plot(t,data);

How to plot the frequency response of butterworth filters using MATLAB

I constructed a code to design 4 filters using butterworth IIR. Low,high,bandpass and bandstop.
Input Diolog window opens up to take in user inputs, I set the default values to :
def = {'5','200','40','50','3','30'}; for Low and High pass
AND
def = {'5','500','60', '200','50','250','3','30'}; for bandpass and bandstop
Order of Filter
Fsampling (Hz)
Fpass (Fpass 1 and 2) (Hz)
Fstop (Fstop 1 and 2) (Hz)
Ripple factors (dB)
Stop attenuation (dB)
for the time being.and using a for loop (1-->4) to calculate B and A components
[n Fn] = buttord(Fpass,Fstop,Rp,Rs);
[B,A] = butter(N,Fn,str);
B_Comp{i} = B;
A_Comp{i} = A;
Now..I need to plot their frequency responses for each filter, so using
for i=1:4
freqz(B_Comp(i),A_Comp(i));
figure;
end
But this error comes up :
??? Function 'fft' is not defined for values of class 'cell'.
Error in ==> fft at 36
[varargout{1:nargout}] = builtin('fft', varargin{:});
Error in ==> freqz at 94
h = dividenowarn(fft(b,s.*nfft),fft(a,s.*nfft)).';
Error in ==> dsp1 at 62
freqz(B_Comp(i),A_Comp(i));
How do i solve this problem.. any help is appreciated
Don't use cells when you want to fft, try before to use cell2mat. For example:
freqz(cell2mat(B_Comp(i)),cell2mat(A_Comp(i)));