2

I need to create a guitar tuner with simulink, which is running on the raspberry pi, but i encountered several problems.

first of all, i got this code to analyze the frequency of a signal:

Fs = 96000;

t = linspace(0,1,Fs);
x = cos(2*pi*4999*t)+randn(size(t));


xdft = fft(x);
fftabs = abs(xdft(1:length(x)/2+1));
freq = 0:Fs/length(x):(Fs/2);
[~,maxindex] = max(fftabs);

mx = freq(maxindex);
res = fftabs;

plot(fftabs);
fprintf('Frequency: %s Hz\n',mx );

and it is doing very well, when i run it, i get the expected result "4999".

but when i am replacing the "x" with the buffered output of the "Audio Device Reader"-Block, i get some strange results or my result equals 0.

In the following image you can see my current model: enter image description here

I am currently testing on windows, so thats why i am not using the "ALSA Audio Capture" block.The MATLAB Function contains following code:

function res = fcn(x)
Fs = 96000;

xdft = fft(x);
fftabs = abs(xdft(1:length(x)/2+1));
freq = 0:Fs/length(x):(Fs/2);
[~,maxindex] = max(fftabs);

mx = freq(maxindex);
res = fftabs;

fprintf('Frequency: %s Hz\n',mx );

end

The next picture contains a visualisation of the input and output from the Matlab Function enter image description here The input was a 440 Hertz tone, but as max value i still get 656hz.

Any ideas, what is causing my problems? Thanks in advance Mick

(I am currently testing on windows, so thats why i am not using the "ALSA Audio Capture" block.)

mick
  • 127
  • 2
  • 11
  • You do not need zero order hold and the buffer block. Delete them and show us code inside MATLAB Function block. Also attach a scope to audio source to make sure your input is not zero. – Navan Nov 18 '16 at 16:19
  • Can you put frequency of input device and some of its data here so someone can check its fft? – huseyin tugrul buyukisik Nov 18 '16 at 18:59
  • thank you both for your help - as requested, i added the code for the function and the diagrams. for the input visualisation i used the "vector scope" from the DSP System Toolbox, the output i plotted in the function with "plot(res);" Also i removed the zero-order hold and the buffer, but the result is still incorrect. – mick Nov 18 '16 at 19:33
  • @Navan Did you mean Mick didn't need the buffer block for his specific case or did you mean the Audio Device Reader is capable of buffering on its own. Trying to use the SPI block and I only want to process after the Raspberry Pi has collected enough data. – Tommy Wolfheart Mar 03 '22 at 14:21
  • In the example Mick provided, Audio Device Reader has "Samples per Frame" set to 1024. Audio device reader will produce 1024 samples output in each step and will have buffers needed to produce this data. – Navan Mar 16 '22 at 18:13

0 Answers0