Cov txheej txheem:

Suab paj nruag Ntes Ntes: 3 Kauj Ruam
Suab paj nruag Ntes Ntes: 3 Kauj Ruam

Video: Suab paj nruag Ntes Ntes: 3 Kauj Ruam

Video: Suab paj nruag Ntes Ntes: 3 Kauj Ruam
Video: Zoo Siab Nrog Koj - Paj Tsua Thoj [ Official MV ] Nkauj Tawm Tshiab 2022 2024, Kaum ib hlis
Anonim
Image
Image

Amaze koj cov phooj ywg thiab tsev neeg nrog txoj haujlwm no uas tshawb pom cov ntawv sau los ntawm cov twj paj nruag. Txoj haujlwm no yuav nthuav tawm kwv yees li ib txwm nrog rau cov suab paj nruag sau ua suab ntawm cov keyboard hluav taws xob, app piano lossis lwm yam twj paj nruag.

Paub meej

Txog qhov haujlwm no, qhov tso tawm analog los ntawm lub suab ntsuas lub ntsuas tau xa mus rau A0 analog input ntawm Arduino Uno. Cov teeb liab analog yog piv txwv thiab ntsuas (digitized). Autocorrelation, qhov hnyav thiab ntsuas tus lej tau siv los nrhiav qhov yooj yim siv thawj 3 lub sijhawm. Qhov kwv yees kwv yees zaus tau muab piv rau cov zaus hauv octaves 3, 4, thiab 5 ntau yam los txiav txim siab cov suab paj nruag ze tshaj plaws. Thaum kawg daim ntawv kwv yees kwv yees rau qhov zaus ze tshaj yog luam tawm rau lub vijtsam.

Nco tseg: Cov lus qhia no tsuas yog tsom mus rau tsim txoj haujlwm. Yog xav paub ntxiv txog cov ntsiab lus thiab tsim kev tsim nyog, thov mus saib qhov txuas no: Cov Lus Qhia Ntxiv

Khoom siv

  • (1) Arduino Uno (lossis Genuino Uno)
  • (1) DEVMO Microphone Sensor Siab Zoo Raug Suab Pom Kev Ua Qauv
  • (1) Solderless Breadboard
  • (1) USB-A rau B Cable
  • Jumper xov hlau
  • Qhov chaw suab paj nruag (piano, keyboard lossis paino app nrog tham)
  • (1) Khoos phis tawj lossis laptop

Kauj Ruam 1: Txhim Kho Cov Khoom Kho Vajtse rau Lub Cim Nco Ntsoov

Teeb tsa Lub Suab Nkauj Ntes Ntes
Teeb tsa Lub Suab Nkauj Ntes Ntes

Siv Arduino Uno, txuas cov xov hluav taws xob, lub lauj kaub tais diav tsis muaj kab thiab DEVMO Microphone Sensor Lub Siab Zoo Raug Suab Pom Kev Pom Zoo (lossis zoo sib xws) tsim cov kab hluav taws xob pom hauv daim duab no

Kauj Ruam 2: Teeb tsa Lub Suab Nkauj Ntes Ntes

Hauv Arduino IDE, ntxiv cov cai hauv qab no.

gistfile1.txt

/*
Cov Ntaub Ntawv/Kos Npe Lub Npe: MusicalNoteDetector
Version No.: V1.0 Tsim 7 Lub Rau Hli, 2020
Thawj Tus Sau: Clyde A. Lettsome, PhD, PE, MEM
Nqe lus piav qhia: Tus lej/daim duab no qhia txog kwv yees li ib txwm nrog rau cov suab paj nruag sau ua suab ntawm cov keyboard hluav taws xob lossis app piano. Rau qhov haujlwm no, cov txiaj ntsig analog los ntawm
lub suab ntsuas lub suab ntsuas tau xa mus rau A0 analog input ntawm Arduino Uno. Cov teeb liab analog yog piv txwv thiab ntsuas (digitized). Autocorrelation, qhov hnyav thiab qhov chaws tau siv
nrhiav qhov tseem ceeb siv thawj 3 lub sijhawm. Qhov kwv yees kwv yees zaus yog piv rau cov zaus hauv octaves 3, 4, thiab 5 kom txiav txim siab cov suab paj nruag ze tshaj
ceeb toom zaus. Thaum kawg daim ntawv kwv yees kwv yees rau qhov zaus ze tshaj yog luam tawm rau lub vijtsam.
Daim ntawv tso cai: Qhov program no yog software pub dawb; koj tuaj yeem faib nws dua thiab/lossis hloov kho nws raws li GNU General Public License (GPL) version 3, lossis yav tom ntej
version uas koj xaiv, raws li luam tawm los ntawm Free Software Foundation.
Sau Ntawv: Copyright (c) 2020 los ntawm C. A Lettsome Services, LLC
Yog xav paub ntxiv mus ntsib
*/
#define SAMPLES 128 // Max 128 rau Arduino Uno.
#define SAMPLING_FREQUENCY 2048 // Fs = Raws li Nyquist, yuav tsum yog 2 npaug ntawm qhov xav tau ntau tshaj plaws.
#define OFFSETSAMPLES 40 // siv rau lub hom phiaj ntsuas ntsuas
#define TUNER -3 // Kho kom txog thaum C3 yog 130.50
Ntim samplingPeriod;
tsis tau kos npe ntev microSeconds;
int X [SAMPLES]; // tsim vector ntawm SAMPLES qhov loj los tuav qhov txiaj ntsig tiag
ntab autoCorr [SAMPLES]; // tsim vector ntawm SAMPLES qhov loj los tuav qhov kev xav
ntab khaws ciaNoteFreq [12] = {130.81, 138.59, 146.83, 155.56, 164.81, 174.61, 185, 196, 207.65, 220, 233.08, 246.94};
int sumOffSet = 0;
int offSet [OFFSETSAMPLES]; // tsim offset vector
koj avgOffSet; // tsim offset vector
int i, k, periodEnd, periodBegin, period, adjuster, noteLocation, octaveRange;
ntab maxValue, minValue;
ntev sum;
int pib = 0;
int numOfCycles = 0;
ntab teeb liabFrequency, signalFrequency2, signalFrequency3, signalFrequencyGuess, tag nrho;
byte xeev_machine = 0;
int samplePerPeriod = 0;
void teeb tsa ()
{
Serial.begin (115200); // 115200 Baud tus nqi rau Serial Monitor
}
void lub voj ()
{
//*****************************************************************
// Tshooj Calabration
//*****************************************************************
Serial.println ("Calabrating. Thov tsis txhob sau cov ntawv thaum lub sijhawm ntsuas.");
rau (kuv = 0; kuv <OFFSETSAMPLES; kuv ++)
{
offSet [kuv] = analogRead (0); // Nyeem tus nqi los ntawm tus lej sib piv 0 (A0), ntsuas nws thiab txuag nws raws li lub sijhawm tiag tiag.
//Serial.println(offSet); // siv qhov no los kho lub suab ntsuas lub ntsuas kom kwv yees li ib nrab lossis 512 thaum tsis muaj suab nrov.
sumOffSet = sumOffSet + offSet [kuv];
}
qauvPerPeriod = 0;
maxValue = 0;
//*****************************************************************
// Npaj lees txais cov tswv yim los ntawm A0
//*****************************************************************
avgOffSet = puag ncig (sumOffSet / OFFSETSAMPLES);
Serial.println ("Suav suav nrog.");
ncua (1000); // ncua rau 1 vib nas this
Serial.println ("3");
ncua (1000); // ncua rau 1 vib nas this
Serial.println ("2");
ncua (1000); // ncua rau 1
Serial.println ("1");
ncua (1000); // ncua rau 1 vib nas this
Serial.println ("Ua si koj li ntawv!");
ncua (250); // ncua rau 1/4 thib ob rau lub sijhawm tshuaj tiv thaiv
//*****************************************************************
// Sau SAMPLES cov qauv los ntawm A0 nrog lub sijhawm piv txwv ntawm samplingPeriod
//*****************************************************************
samplingPeriod = 1.0 / SAMPLING_FREQUENCY; // Lub sij hawm hauv microseconds
rau (kuv = 0; kuv <SAMPLES; kuv ++)
{
microSeconds = micros (); // Rov qab tus naj npawb ntawm microseconds txij li Arduino pawg thawj coj pib khiav cov ntawv tam sim no.
X = analogRead (0); // Nyeem tus nqi los ntawm tus lej sib piv 0 (A0), ntsuas nws thiab txuag nws raws li lub sijhawm tiag tiag.
/ *tshuav sijhawm tos ntawm cov qauv yog tias tsim nyog hauv vib nas this */
thaum (micros () <(microSeconds + (samplingPeriod * 1000000)))
{
// tsis ua dab tsi tsuas yog tos
}
}
//*****************************************************************
// Autocorrelation Function
//*****************************************************************
rau (kuv = 0; kuv <SAMPLES; kuv ++) // kuv = ncua
{
sum = 0;
rau (k = 0; k <SAMPLES - i; k ++) // Sib tw teeb liab nrog cov teeb liab qeeb
{
sum = sum + (((X [k]) - avgOffSet) * ((X [k + i]) - avgOffSet)); // X [k] yog lub teeb liab thiab X [k+i] yog qhov ncua sijhawm
}
autoCorr = sum / SAMPLES;
// Thawj Peak Tshawb Xyuas Lub Xeev Tshuab
yog (state_machine == 0 && i == 0)
{
pib = autoCorr * 0.5;
xeev_machine = 1;
}
lwm tus yog (state_machine == 1 && i> 0 && thresh 0) // state_machine = 1, nrhiav 1 lub sijhawm rau siv thawj lub voj voog
{
maxValue = autoCorr [kuv];
}
lwm tus yog (state_machine == 1 && i> 0 && thresh <autoCorr [i-1] && maxValue == autoCorr [i-1] && (autoCorr -autoCorr [i-1]) <= 0)
{
pibBegin = i-1;
xeev_machine = 2;
numOfCycles = 1;
samplePerPeriod = (periodBegin - 0);
sij hawm = piv txwvPerPeriod;
adjuster = TUNER+(50.04 * exp (-0.102 * samplePerPeriod));
signalFrequency = ((SAMPLING_FREQUENCY) / (samplePerPeriod))-txiav txim siab; // f = fs/N
}
lwm tus yog (state_machine == 2 && i> 0 && thresh 0) // state_machine = 2, nrhiav 2 lub sijhawm rau lub hlis thib ib thiab thib ob
{
maxValue = autoCorr [kuv];
}
lwm tus yog (state_machine == 2 && i> 0 && thresh <autoCorr [i-1] && maxValue == autoCorr [i-1] && (autoCorr -autoCorr [i-1]) <= 0)
{
periodEnd = i-1;
xeev_machine = 3;
numOfCycles = 2;
samplePerPeriod = (periodEnd - 0);
signalFrequency2 = ((numOfCycles*SAMPLING_FREQUENCY) / (samplePerPeriod))-txiav txim siab; // f = (2*fs)/(2*N)
maxValue = 0;
}
lwm tus yog (state_machine == 3 && i> 0 && thresh 0) // state_machine = 3, nrhiav 3 lub sijhawm rau 1st, 2nd thiab 3rd cycle
{
maxValue = autoCorr [kuv];
}
lwm tus yog (state_machine == 3 && i> 0 && thresh <autoCorr [i-1] && maxValue == autoCorr [i-1] && (autoCorr -autoCorr [i-1]) <= 0)
{
periodEnd = i-1;
xeev_machine = 4;
numOfCycles = 3;
samplePerPeriod = (periodEnd - 0);
signalFrequency3 = ((numOfCycles*SAMPLING_FREQUENCY) / (samplePerPeriod))-txiav txim siab; // f = (3*fs)/(3*N)
}
}
//*****************************************************************
// Kev Ntsuam Xyuas Tshwm Sim
//*****************************************************************
yog (cov qauvPerPeriod == 0)
{
Serial.println ("Hmm ….. Kuv tsis paub tseeb. Koj puas tau sim dag kuv?");
}
lwm tus
{
// npaj qhov hnyav ua haujlwm
tag nrho = 0;
yog (signalFrequency! = 0)
{
tag nrho = 1;
}
yog (signalFrequency2! = 0)
{
tag nrho = tag nrho + 2;
}
yog (signalFrequency3! = 0)
{
tag nrho = tag nrho + 3;
}
// suav qhov zaus siv qhov hnyav ua haujlwm
signalFrequencyGuess = ((1/tag nrho) * signalFrequency) + ((2/tag nrho) * signalFrequency2) + ((3/tag nrho) * signalFrequency3); // nrhiav qhov hnyav ntau zaus
Serial.print ("Cov ntawv koj tau ua yog kwv yees");
Serial.print (signalFrequencyGuess); // Luam tawm qhov kwv yees kwv yees.
Serial.println ("Hz.");
// nrhiav thaj tsam octave raws li kev kwv yees
octaveRange = 3;
thaum (! (signalFrequencyGuess> = khawsNoteFreq [0] -7 && signalFrequencyGuess <= khawsNoteFreq [11] +7))
{
rau (kuv = 0; kuv <12; kuv ++)
{
khaws ciaNoteFreq [kuv] = 2 * khaws ciaNoteFreq [kuv];
}
octaveRange ++;
}
// Nrhiav cov ntawv ze tshaj
minValue = 10000000;
noteLocation = 0;
rau (kuv = 0; kuv <12; kuv ++)
{
yog (minValue> abs (signalFrequencyGuess-storedNoteFreq ))
{
minValue = abs (signalFrequencyGuess-storedNoteFreq );
noteLocation = kuv;
}
}
// Luam daim ntawv
Serial.print ("Kuv xav tias koj tau ua si");
yog (noteLocation == 0)
{
Serial.print ("C");
}
lwm yog (noteLocation == 1)
{
Serial.print ("C#");
}
lwm yog (noteLocation == 2)
{
Serial.print ("D");
}
lwm yog (noteLocation == 3)
{
Serial.print ("D#");
}
lwm yog (noteLocation == 4)
{
Serial.print ("E");
}
lwm yog (noteLocation == 5)
{
Serial.print ("F");
}
lwm yog (noteLocation == 6)
{
Serial.print ("F#");
}
lwm yog (noteLocation == 7)
{
Serial.print ("G");
}
lwm yog (noteLocation == 8)
{
Serial.print ("G#");
}
lwm yog (noteLocation == 9)
{
Serial.print ("A");
}
lwm yog (noteLocation == 10)
{
Serial.print ("A#");
}
lwm yog (noteLocation == 11)
{
Serial.print ("B");
}
Serial.println (octaveRange);
}
//*****************************************************************
Nres ntawm no. Ntaus khawm pib dua ntawm Arduino kom rov pib dua
//*****************************************************************
thaum (1);
}

saib rawgistfile1.txt tuav nrog ❤ los ntawm GitHub

Kauj Ruam 3: Teeb tsa Lub Suab Nkauj Ntes Ntes

Txuas Arduino Uno rau PC nrog cov lej sau lossis thauj khoom hauv Arduino IDE. Sau thiab xa cov cai rau Arduino. Muab lub vojvoog kaw rau ntawm qhov chaw ntaus nkauj. Nco tseg: Hauv cov vis dis aus qhia, Kuv siv daim ntawv thov teeb tsa ntawm cov ntsiav tshuaj ua ke nrog lub khoos phis tawj hais lus raws li kuv lub suab paj nruag. Ntaus lub pob pib dua ntawm Arduino Board thiab tom qab ntawd ua cov ntawv ntawm cov nkauj. Tom qab ob peb vib nas this, Lub Suab Cim Ntes Ntes yuav tso saib cov ntawv ua si thiab nws zaus.

Pom zoo: